Add the criterion dependency to your Cargo.toml, create a benches directory with a benchmark file, and run cargo bench with the walltime feature enabled.
- Add
criterionto[dev-dependencies]inCargo.tomlwith thecargo_bench_supportfeature. - Create a
benchesdirectory and add a file likebenches/float_add.rscontaining your benchmark logic. - Define a
[[bench]]section inCargo.tomlwithharness = falseandrequired-features = ["walltime"]. - Run the benchmarks using
cargo bench --features walltime.
[dev-dependencies]
criterion = { version = "0.8.2", default-features = false, features = ["cargo_bench_support"] }
[[bench]]
name = "float_add"
harness = false
required-features = ["walltime"]
[features]
walltime = ["dep:criterion"]
use criterion::{Criterion, criterion_main};
fn my_function() {
// Code to benchmark
}
fn bench_my_function(c: &mut Criterion) {
c.bench_function("my_function", |b| b.iter(my_function));
}
criterion_main!(bench_my_function);
cargo bench --features walltime