Skip to content

Instantly share code, notes, and snippets.

@connorslade
Created August 22, 2021 17:52
Show Gist options
  • Save connorslade/ec2f038d502751cae179bb54daa23684 to your computer and use it in GitHub Desktop.
Save connorslade/ec2f038d502751cae179bb54daa23684 to your computer and use it in GitHub Desktop.
Stable Rust Benchmarker
fn bencher(name: &str, iter: u32, f: fn() -> ()) {
let mut avg_time: u128 = 0;
for i in 0..iter {
let start = std::time::Instant::now();
f();
avg_time += start.elapsed().as_nanos();
}
println!("[*] Bench: {} ({})", name, iter);
println!(" ├─ Total: {}ns", avg_time);
println!(" └─ AVG: {}ns\n", avg_time / iter as u128);
}
// Useage
bencher("Do random stuff", 1000, || {
let mut a = 1;
for i in 0..100 {
a *= 2;
}
});
// Output
// [*] Bench: Do random stuff (1000)
// ├─ Total: 33156ns
// └─ AVG: 33ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment