Skip to content

Instantly share code, notes, and snippets.

@BD103

BD103/lib.rs Secret

Created April 16, 2024 13:03
Show Gist options
  • Save BD103/cb7becebf2509accbd31973c73d7ad0d to your computer and use it in GitHub Desktop.
Save BD103/cb7becebf2509accbd31973c73d7ad0d to your computer and use it in GitHub Desktop.
Benchmarking `#[track_caller]`
pub fn untracked(x: bool) {
if x {
panic!("Untracked");
}
}
#[track_caller]
pub fn tracked(x: bool) {
if x {
panic!("Tracked");
}
}
$ cargo bench
...
tracked false time: [1.5369 ns 1.5376 ns 1.5383 ns]
Found 8 outliers among 100 measurements (8.00%)
5 (5.00%) high mild
3 (3.00%) high severe
untracked false time: [1.5371 ns 1.5376 ns 1.5382 ns]
Found 5 outliers among 100 measurements (5.00%)
3 (3.00%) high mild
2 (2.00%) high severe
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use playground::*;
fn bench(c: &mut Criterion) {
c
.bench_function("tracked false", |b| b.iter(|| tracked(black_box(false))))
.bench_function("untracked false", |b| b.iter(|| untracked(black_box(false))));
// c.bench_function("tracked true", |b| b.iter(|| tracked(black_box(true))))
// .bench_function("untracked true", |b| b.iter(|| untracked(black_box(true))));
}
criterion_group!(benches, bench);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment