Sub topic 10
Tuning sample size and measurement time
This chapter assumes you can already write a Criterion benchmark with bench_function and iter, and that you know what a benchmark group is. It covers the two settings you will change most often: how many samples Criterion collects and how long it spends collecting them.
What the two settings control
Criterion measures a function in three phases. First it runs a warm-up. Then it collects a fixed number of samples. Finally it analyzes them.
- Sample size is the number of samples. The default is 100.
- Measurement time is the target time for the whole sampling phase. The default is 5 seconds.
- Warm-up time is how long Criterion runs the code before it measures anything. The default is 3 seconds.
A sample is not one call. Criterion estimates how long one call takes during warm-up. It then picks an iteration count for each sample so that all samples together fit in the measurement time. A fast function gets many iterations per sample. A slow function gets few.
Setting values for every benchmark
Use the named form of criterion_group! to set a configuration for all targets in the group.
use std::time::Duration;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn work(n: u64) -> u64 {
(0..n).sum()
}
fn bench_work(c: &mut Criterion) {
c.bench_function("sum 10000", |b| b.iter(|| work(black_box(10_000))));
}
criterion_group! {
name = benches;
config = Criterion::default()
.sample_size(50)
.warm_up_time(Duration::from_secs(1))
.measurement_time(Duration::from_secs(10));
targets = bench_work
}
criterion_main!(benches);This asks for 50 samples spread over 10 seconds, after a 1 second warm-up. Each sample then runs roughly 0.2 seconds of work.
Setting values for one group
A single slow benchmark should not force its settings on the rest. Set the values on a benchmark group instead.
use std::time::Duration;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn slow_work(n: u64) -> u64 {
(0..n).map(|x| x.wrapping_mul(x)).sum()
}
fn bench_slow(c: &mut Criterion) {
let mut group = c.benchmark_group("slow");
group.sample_size(10);
group.measurement_time(Duration::from_secs(20));
group.bench_function("square sum 50M", |b| {
b.iter(|| slow_work(black_box(50_000_000)))
});
group.finish();
}
criterion_group!(benches, bench_slow);
criterion_main!(benches);Group settings apply only to benchmarks created from that group.
Overriding from the command line
Arguments after -- go to Criterion, not to Cargo. They are handy for a quick experiment without editing code.
cargo bench -- --sample-size 20 --measurement-time 3 --warm-up-time 1Choosing values
More samples and more time give narrower confidence intervals. They also make the run slower. Use these rules of thumb:
| Situation | Adjustment |
|---|---|
| Result interval is wide and you need precision | Raise `measurement_time` first, then `sample_size` |
| Each call takes seconds | Lower `sample_size` to 10 and raise `measurement_time` |
| Many benchmarks make the suite slow | Lower `measurement_time` for the less important ones |
| Timings drift during the first seconds (caches, CPU frequency) | Raise `warm_up_time` |
Criterion requires a sample size of at least 10. A smaller value causes a panic.
Common mistakes
- Cutting
sample_sizeto speed up a suite, then reading the wider interval as a real slowdown. Fewer samples give less certain statistics, not a slower program. - Changing settings between a saved baseline run and the comparison run. Differences in the statistics can then come from the setup, not from your code change.
- Setting a very long
measurement_timeon every benchmark. Raise it only where the confidence interval is too wide to be useful.
Exercise
A benchmark calls a function that takes about 2 seconds per call. With the defaults, Criterion warns that it cannot complete 100 samples in 5 seconds. Name two settings changes that fix the warning, and say which setting has a hard lower limit.
Show answer
Lower sample_size (for example to 10) or raise measurement_time (to at least the time the warning suggests), or both. sample_size has a hard lower limit of 10, and values below 10 cause a panic.