Created
August 22, 2021 17:52
-
-
Save connorslade/ec2f038d502751cae179bb54daa23684 to your computer and use it in GitHub Desktop.
Stable Rust Benchmarker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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