Sub topic 5
How Criterion measures: warm-up, samples, and statistics
This chapter explains what happens between cargo bench and the numbers Criterion prints. It assumes you can write a basic Criterion benchmark with bench_function and Bencher::iter, and that you know why harness = false is set in Cargo.toml. Knowing the measurement model helps you pick sensible settings and spot results you shouldn't trust.
The four phases of a benchmark
Criterion runs each benchmark through the same phases. A single call to your closure is too short to time reliably, so Criterion measures many calls at once and divides.
- Warm-up. Criterion runs the routine repeatedly for a fixed time (3 seconds by default). This fills CPU caches, trains the branch predictor, and lets the clock frequency settle. It also gives Criterion a first estimate of how long one iteration takes.
- Planning. Using that estimate, Criterion chooses how many iterations each sample will contain, so that all samples together fit in the measurement time (5 seconds by default).
- Sampling. Criterion collects 100 samples by default. A sample is one timing of a batch of iterations. Early samples use few iterations and later ones use more, in a linear progression.
- Analysis. Criterion fits a statistical model to the samples and reports an estimate with a confidence interval. If a previous run exists, it also tests whether performance changed.
Why samples contain many iterations
Timing one call of a fast function mostly measures the timer itself. Instead, a sample times a loop of n iterations and records the total. Sample i might run i * d iterations for some step d. Each sample is therefore a point (iterations, total time).
Criterion then fits a line through those points with linear regression. The slope of the line is the time per iteration. Fixed overhead, such as the cost of starting and stopping the timer, shows up in the intercept and does not distort the slope. This is why Criterion usually reports a tighter estimate than a plain average of single calls.
What the confidence interval means
The console output shows three numbers, for example [12.31 µs 12.38 µs 12.46 µs]. These are the lower bound, the point estimate, and the upper bound of a 95% confidence interval by default. Criterion computes the interval with a bootstrap. It resamples your collected samples many times (100,000 resamples by default), recomputes the slope each time, and takes the spread of those results.
A narrow interval means the samples agreed with each other. A wide interval means the machine was noisy or the function's runtime varies. The interval describes the uncertainty of the estimate under the measurement conditions. It does not guarantee that a different machine would give the same time.
Change detection
When a previous result exists in target/criterion, Criterion compares the new samples against the old ones. It runs a hypothesis test and reports a p-value, then compares the estimated change against a noise threshold (2% by default). Two settings control this:
significance_level(default 0.05) is the p-value below which a difference counts as real.noise_threshold(default 0.01 in the library, applied as a minimum size of change) filters out changes too small to matter.
Criterion prints "Performance has regressed", "Performance has improved", or "No change in performance detected". A change can be statistically significant yet smaller than the noise threshold. In that case Criterion reports no meaningful change.
Seeing the settings in code
You can set each of these values on the Criterion object. This benchmark shortens warm-up and raises the confidence level:
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()
.warm_up_time(Duration::from_secs(1))
.confidence_level(0.99)
.nresamples(50_000);
targets = bench_work
}
criterion_main!(benches);A higher confidence level widens the interval, because a wider range is needed to be 99% sure it contains the true value.
Common misreadings
- The middle number is a regression estimate, not the minimum observed time.
- Outliers are flagged but not removed from the estimate. A high share of outliers points to noise, not to a faster or slower function.
- A wide interval after a change means the comparison is weak, even if the point estimate moved.
Exercise
Criterion times each sample as a batch of iterations and fits a line through (iterations, total time) points. What does the slope of that line represent, and why is this better than timing one call at a time?
Show answer
The slope is the estimated time per iteration. Fixed costs like timer overhead end up in the intercept, so they do not inflate the per-iteration figure. Timing single calls of a very fast function would mostly measure that overhead.
Exercise
You set confidence_level(0.99) and the reported interval gets wider than before. Did your code get slower or less stable?
Show answer
Not necessarily. A higher confidence level requires a wider interval to be that sure it contains the true value. Compare intervals only when they use the same confidence level.