Debian Codegraph @

debian/rust-vendor/codspeed-divan-compat/benches/drop_example.rs

raw
1 use codspeed_divan_compat::Bencher;
2
3 struct LargeInput {
4 data: Vec<u8>,
5 }
6
7 impl Drop for LargeInput {
8 #[inline(never)]
9 fn drop(&mut self) {
10 // Simulate a large drop by performing some computation
11 let sum: u8 = self.data.iter().copied().sum();
12 std::hint::black_box(sum); // Prevent optimization
13 }
14 }
15
16 impl LargeInput {
17 fn new() -> Self {
18 Self {
19 data: vec![42; 1024 * 1024 /* 1MiB */],
20 }
21 }
22
23 fn process(&self) -> u64 {
24 // Simulate some work on the data
25 std::thread::sleep(std::time::Duration::from_millis(50));
26 self.data.iter().map(|&x| x as u64).sum()
27 }
28 }
29
30 #[codspeed_divan_compat::bench]
31 fn bench_large_input(bencher: Bencher) {
32 bencher
33 .with_inputs(LargeInput::new)
34 .bench_values(|input| input.process());
35 }
36
37 #[codspeed_divan_compat::bench]
38 fn bench_large_input_local(bencher: Bencher) {
39 let input = LargeInput::new();
40 bencher.bench_local(|| input.process());
41 }
42
43 fn main() {
44 codspeed_divan_compat::main();
45 }