Skip to content

Commit e476e55

Browse files
committed
feat(divan_compat): add drop example
1 parent 4be2e55 commit e476e55

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

crates/divan_compat/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ harness = false
2929
[[bench]]
3030
name = "sleep_benches"
3131
harness = false
32+
33+
[[bench]]
34+
name = "drop_example"
35+
harness = false
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)