Criterion for benchmarking with statistical analysis
Marcus Chen
Jan 2026
1 tab
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}
fn bench_fib(c: &mut Criterion) {
c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
}
criterion_group!(benches, bench_fib);
criterion_main!(benches);
1 file · rust
Explain with highlit
Criterion is the standard benchmarking library for Rust. It runs benchmarks multiple times, detects outliers, and reports statistical confidence intervals. The API is simple: wrap your code in a closure, and criterion measures execution time. It generates HTML reports with graphs showing performance over time. I use criterion to compare implementations, detect regressions, and validate optimizations. The library handles warmup, calibration, and noise reduction automatically. For micro-benchmarks, it's more reliable than cargo bench (which uses the unstable test::Bencher). Criterion also supports parameterized benchmarks and throughput measurements. The reports make it easy to communicate performance characteristics to stakeholders.