Benchmarking
This crate can be used to execute something and measure the execution time in nano seconds. It does not output anything to the screen and the filesystem.
Examples
extern crate benchmarking;
use benchmarking::Benchmark;
fn main() {
const VEC_LENGTH: usize = 100;
let mut benchmark = Benchmark::default();
benchmark.warm_up();
let bench_result = benchmark.bench_function(|measurer| {
let mut vec: Vec<usize> = Vec::with_capacity(VEC_LENGTH);
measurer.measure(|| {
for i in 0..VEC_LENGTH {
vec.push(i);
}
});
}).unwrap();
println!("Filling 0 to 99 into a vec takes {} nano seconds!", bench_result);
}
extern crate benchmarking;
use benchmarking::Benchmark;
fn main() {
const VEC_LENGTH: usize = 100;
let mut benchmark = Benchmark::default();
benchmark.warm_up();
let bench_result = benchmark.bench_function(|measurer| {
let mut vec: Vec<usize> = Vec::with_capacity(VEC_LENGTH);
measurer.measure_for_loop(0..VEC_LENGTH, |loop_seq| {
vec.push(loop_seq);
});
}).unwrap();
println!("Pushing a number into a vec takes {} nano seconds!", bench_result);
}
extern crate benchmarking;
use benchmarking::Benchmark;
fn main() {
const VEC_LENGTH: usize = 100;
let mut benchmark = Benchmark::default();
benchmark.warm_up();
let bench_result = benchmark.bench_function(|measurer| {
let mut vec: Vec<usize> = Vec::with_capacity(VEC_LENGTH);
measurer.measure_while_loop(|loop_seq| {
loop_seq < VEC_LENGTH
}, |loop_seq| {
vec.push(loop_seq);
});
}).unwrap();
println!("Pushing a number into a vec takes {} nano seconds!", bench_result);
}
The warm_up
and warm_up_with_duration
methods of a Benchmark
instance runs on one thread. To warm up all CPUs, you can use the warm_up_multi_thread
and warm_up_multi_thread_with_duration
methods instead.
Crates.io
https://crates.io/crates/benchmarking
Documentation
https://docs.rs/benchmarking
License
MIT