A quick scratchpad for benchmarking Rust code
- cargo-make -
cargo install --force cargo-make
- Edit benches/benchmark.rs
- Run
cargo make bench
The benchmarks!
macro accepts input with the following structure:
benchmarks! {
benchmark_name { /* benchmark impl */ }
other_benchmark_name { /* other benchmark impl */ }
}
The criterion.rs black_box
function is included and usable within the macro.
criterion::black_box
stops the compiler from constant-folding away the whole function and replacing it with a constant.
You can you the keyword @skip
before a benchmark for it not to be run:
benchmarks! {
@skip benchmark_name { /* benchmark impl */ }
}
cargo make bench
- runs the benchmark viacargo-criterion
cargo make bench-verbose
- runs the benchmark viacargo-criterion
with verbose outputcargo make reset
- resets the criterion data for earlier benchmarkscargo make new
- returns benches/benchmark.rs to the initial statecargo make save [FILENAME]
- saves the contents of benches/benchmark.rs tosaved/[FILENAME].rs
mod internals;
benchmarks! {
collect_vec_string {
let vector = black_box(vec!["hello", "world"]);
let _output = vector
.iter()
.map(|item| item.to_uppercase())
.collect::<Vec<String>>()
.join("");
}
collect_string {
let vector = black_box(vec!["hello", "world"]);
let _output = vector
.iter()
.map(|item| item.to_uppercase())
.collect::<String>();
}
}