How to benchmark with criterion

Add criterion to dev-dependencies, create a harness=false bench file, and run cargo bench with the walltime feature.

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.

  1. Add criterion to [dev-dependencies] in Cargo.toml with the cargo_bench_support feature.
  2. Create a benches directory and add a file like benches/float_add.rs containing your benchmark logic.
  3. Define a [[bench]] section in Cargo.toml with harness = false and required-features = ["walltime"].
  4. 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