skip to content

In a Rust criterion benchmark run with --baseline main, what do the change interval and p-value mean, and when is a change only noise?

level: middleimportance: should knowfreq 25%

answer

  1. two tests, both must pass
  2. t-test on per-iteration times
  3. bootstrap interval of the mean change
  4. the whole interval beyond one percent
  5. save, compare, do not overwrite

basics

~20 s

Criterion runs a t-test against the saved baseline and bootstraps a 95% interval for the relative change in mean time; by default, improved or regressed needs p below 0.05 and the whole interval beyond ±1%.

solid answer

~40 s

By default each benchmark warms up for 3 s, takes 100 samples in about 5 s, and bootstraps with 100,000 resamples. The `change:` line gives the lower bound, point estimate and upper bound of a 95% interval for the relative change in mean time, followed by a t-test p-value. If p is not below 0.05, criterion prints `No change in performance detected.`; if it is, the interval must lie entirely below −1% for `Performance has improved.` or above +1% for `Performance has regressed.`, otherwise `Change within noise threshold.`. Unchanged `fnv1a` showed `[+0.8758% +2.1708% +3.3680%] (p = 0.00 < 0.05)` and was judged noise, because the lower bound sat under 1%. `--save-baseline main` stores a run under that name, and `--baseline main` compares with it without overwriting it.

code

rust · 17 lines
rust
use std::hint::black_box;

use checksums::{adler32, fnv1a};
use criterion::{Criterion, Throughput, criterion_group, criterion_main};

fn checksums_64k(c: &mut Criterion) {
    let buf: Vec<u8> = (0..64 * 1024).map(|i| (i % 251) as u8).collect();

    let mut group = c.benchmark_group("checksum-64KiB");
    group.throughput(Throughput::Bytes(buf.len() as u64));
    group.bench_function("adler32", |b| b.iter(|| adler32(black_box(&buf))));
    group.bench_function("fnv1a", |b| b.iter(|| fnv1a(black_box(&buf))));
    group.finish();
}

criterion_group!(benches, checksums_64k);
criterion_main!(benches);

go deeper

for a junior

Recall that criterion saves each run and compares the next one with it, and that the three bracketed numbers are an interval around an estimate.

for a middle

Explain the two-step verdict: the t-test against 0.05, then the whole change interval against the 1% noise threshold, and the four sentences that result.

for a senior

Use named baselines deliberately, recognise a significant but tiny change as machine drift, and know which flags compare without overwriting.

for a principal

Decide which significance and noise settings a team's comparisons use, based on how noisy its benchmark machines really are.

## What one criterion run measures For each benchmark, criterion 0.7 runs four phases with these defaults: 1. **Warm-up** for 3 seconds, calling the routine with doubling iteration counts, to settle caches and frequency scaling and estimate the time per iteration. 2. **Sampling**: 100 samples spread over about 5 seconds. For short routines each sample runs a growing number of iterations (linear sampling); for long ones every sample runs the same number (flat sampling). 3. **Outlier classification** with Tukey's fences: a sample beyond 1.5 interquartile ranges from the quartiles is a *mild* outlier, beyond 3 is *severe*. Criterion reports them and keeps them in the analysis. 4. **Bootstrap**: 100,000 resamples of the sample give 95% confidence intervals for the statistics. The bracketed numbers on the `time:` line are the lower bound, the point estimate and the upper bound of the **typical** estimate: the slope of time against iterations in linear sampling, or the mean in flat sampling. ## Reading the comparison lines | Line | Meaning | |---|---| | `time: [a b c]` | 95% interval and estimate for this run's time per iteration | | `thrpt: [...]` | the same converted to bytes per second, when the group declares a `Throughput` | | `change: time: [x y z] (p = ...)` | interval for the relative change in mean time against the baseline, and the t-test p-value | | `change: thrpt: [...]` | the same change expressed as throughput, computed as `1 / (1 + r) - 1` | | verdict | one of four sentences, chosen by the rule below | ## The verdict rule Criterion applies two tests in sequence, using a **significance level** of 0.05 and a **noise threshold** of 0.01 by default: - p-value not below 0.05: `No change in performance detected.` - p-value below 0.05 and the whole change interval below −1%: `Performance has improved.` - p-value below 0.05 and the whole interval above +1%: `Performance has regressed.` - p-value below 0.05 but the interval touching the ±1% band: `Change within noise threshold.` The second test exists because a t-test over 100 samples detects very small shifts, and a machine's state drifts. In the example output, `fnv1a` was not touched between the two runs, yet the t-test gave `p = 0.00`; the interval `[+0.8758% +2.1708% +3.3680%]` started below +1%, so criterion called it noise. The rewritten `adler32` moved by −88% with the whole interval far below −1%, so it was reported as improved. Both levels can be changed per run (`--noise-threshold`, `--significance-level`) or in code on a `Criterion` or a `BenchmarkGroup`. ## Baselines on disk Criterion stores results under `target/criterion/<group>/<benchmark>/`. Without flags, every run compares against the directory named `base` and then overwrites it, so each run is compared with the previous one. Named baselines make the comparison deliberate. The flags go to the bench binary, after Cargo's `--` separator: | Flag | Effect | |---|---| | `--save-baseline main` | save this run as `main`, comparing with the old `main` if one exists | | `--baseline main` | compare with `main` and do not overwrite it; panics if `main` is missing | | `--baseline-lenient main` | compare where `main` exists and skip the comparison elsewhere | | `--discard-baseline` | do not save this run | | `--load-baseline feature --baseline main` | compare the saved `feature` results with `main` without sampling again | ## Changing the settings The same defaults can be changed at three levels, and interviewers expect a candidate to know they exist rather than to memorise them: - **Command line**, for one run: `--sample-size`, `--warm-up-time`, `--measurement-time`, `--noise-threshold`, `--significance-level`, `--confidence-level`. `--quick` stops sampling once the significance level has been reached. - **The whole harness**, through `criterion_group!(name = benches; config = Criterion::default().sample_size(50); targets = ...)`. Flags given on the command line are applied after that `config` expression. - **One group**, through `BenchmarkGroup` methods such as `sample_size`, `measurement_time` or `noise_threshold`. `sample_size` asserts that the value is at least 10. A longer measurement narrows the interval for a stable routine; it does not remove noise that comes from the machine. ## Groups and throughput `c.benchmark_group("checksum-64KiB")` creates a `BenchmarkGroup` whose benchmarks share settings and a report. `group.throughput(Throughput::Bytes(buf.len() as u64))` tells criterion how many bytes one iteration processes, which adds the `thrpt:` lines; `Throughput::Elements` counts items instead. `group.finish()` ends the group. - Compare **like with like**: same machine, same power state, same build profile. - Read the **interval**, not just the middle number. - Treat many **severe outliers** as a sign of interference from other processes.

  • Why did the unchanged fnv1a get p = 0.00?
    With 100 samples on each side, the t-test is sensitive enough to detect a shift of a couple of percent caused by the machine itself: clock frequency, temperature, background processes. Statistical significance therefore cannot mean a real regression on its own, which is why criterion also requires the whole interval to clear its 1% noise threshold. On a noisy machine, raise that threshold with `--noise-threshold`.
  • Which number is the headline time criterion prints?
    The typical estimate: in linear sampling, the default for short routines, it is the slope of a regression of sample time against iteration count; in flat sampling it is the mean. The outer two numbers in the brackets are the lower and upper bounds of its 95% confidence interval, not a minimum and maximum.
  • What happens if you run with --baseline main and one benchmark has never been saved under main?
    The run panics with `Baseline 'main' must exist before comparison is allowed; try --save-baseline main`, and Cargo reports the bench target as failed. `--baseline-lenient main` compares the benchmarks that have a saved `main` and skips the comparison for the rest.

  • p below 0.05 means the code really got slower.
  • Change within noise threshold means the p-value was above 0.05.
  • --baseline main saves the new results over the main baseline.
  • Criterion throws outliers away before it computes the estimate.
  • The middle number on the time line is the median iteration time.