Sub topic 11
Saving and comparing baselines
This chapter assumes you have a working Criterion benchmark target (harness = false), that you can read the time: [low mid high] line in the console output, and that you know what the change: line means. You also need basic Git, because the workflow compares two versions of the code.
What a baseline is
A baseline is a saved set of measurements for a benchmark. Criterion writes it to disk so a later run can compare itself against it.
Every cargo bench run already uses baselines, without your asking. Criterion stores results under target/criterion/, in one directory per benchmark. Inside each benchmark directory you will find subdirectories such as these:
newholds the measurements from the most recent run.baseholds the measurements from the run before that.changeholds the estimated difference between the two.
On each run, Criterion moves the old new data to base, measures again, writes the result to new, and compares the two. That comparison produces the change: line.
This automatic comparison is fine for quick feedback. It has two weaknesses:
- It only looks one run back. An extra run on unchanged code replaces
base, and the measurement you wanted to compare against is gone. - You cannot name the comparison point. You cannot say "compare against the code on
main".
Named baselines fix both problems.
Saving and comparing with named baselines
Two flags drive the workflow. You pass them after --, so Cargo hands them to the Criterion binary instead of interpreting them itself.
--save-baseline <name>runs the benchmarks and stores the results under<name>.--baseline <name>runs the benchmarks and compares them against the baseline called<name>.
The spine example shows the whole cycle:
cargo bench
cargo bench -- --save-baseline before
# change the code, then compare against the saved run
cargo bench -- --baseline beforeThe first command is an ordinary run. The second saves a baseline named before. After you edit the code, the third command measures the new code and reports the difference from before.
You can compare against before as many times as you like. Using --baseline does not modify the saved baseline. Only --save-baseline overwrites it, and it overwrites any existing baseline with the same name.
A worked example
Take the recursive fibonacci benchmark from earlier chapters and save a baseline before changing anything. Start from benches/fib_bench.rs:
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn fibonacci(n: u64) -> u64 {
match n {
0 | 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);Save the baseline:
cargo bench --bench fib_bench -- --save-baseline recursiveNow replace the function body with an iterative version. The function keeps the same name and signature, so the benchmark code does not change:
fn fibonacci(n: u64) -> u64 {
let (mut a, mut b) = (1u64, 1u64);
for _ in 1..n {
let next = a + b;
a = b;
b = next;
}
b
}This version returns the same values as the recursive one for n >= 1 (both give 1 for n = 1, and 10946 for n = 20). Compare against the saved baseline:
cargo bench --bench fib_bench -- --baseline recursiveExpect output similar to this, with different numbers on your machine:
fib 20 time: [6.1 ns 6.2 ns 6.3 ns]
change: [-99.9% -99.9% -99.9%] (p = 0.00 < 0.05)
Performance has improved.The change: line now measures against recursive, not against whatever ran last. You could run the benchmark ten more times and the reference would stay the same.
Reading a comparison
The change: line gives a confidence interval for the relative change in the typical time, as [low estimate high]. A negative number means the new code is faster. Criterion then prints one of these verdicts:
| Verdict | Meaning |
|---|---|
| Performance has improved. | The difference is statistically significant and exceeds the noise threshold, in the faster direction. |
| Performance has regressed. | Same test, in the slower direction. |
| Change within noise threshold. | The difference is statistically detectable but smaller than the noise threshold. |
| No change in performance detected. | The p-value is not below the significance level. |
Two settings control the verdict:
- Significance level, 0.05 by default. If the p-value is below it, Criterion treats the difference as real rather than random variation. You can lower it for stricter tests.
- Noise threshold, 0.01 (1%) by default. A statistically significant change smaller than this is reported as noise.
You can set both in the Criterion configuration, using .significance_level(0.01) and .noise_threshold(0.03), or pass --significance-level and --noise-threshold on the command line. On a noisy machine, raising the noise threshold to 3% to 5% cuts down on false alarms. Remember that a significant p-value tells you a difference exists. It does not tell you the difference matters for your program. A 1.2% slowdown in a function that takes 0.1% of your runtime is real but unimportant.
The HTML report also includes the comparison. Open target/criterion/report/index.html and click a benchmark. The page shows the new and old distributions overlaid, along with the change estimate.
Comparing two saved baselines without re-running
Sometimes you want to compare two earlier runs, for example results from two branches. You do not need to measure again. Use --load-baseline to load saved data instead of sampling, together with --baseline to pick the comparison point:
cargo bench --bench fib_bench -- --save-baseline main
git checkout my-feature
cargo bench --bench fib_bench -- --save-baseline feature
# compare the two saved runs without measuring again
cargo bench --bench fib_bench -- --load-baseline feature --baseline mainThis is useful when you collected both baselines in one session and want to review the comparison later. It also makes the result reproducible, because the comparison no longer depends on machine noise at the time you ask.
Branch-based workflow
A reliable way to check a change for regressions uses the same machine and the same session for both measurements:
- Check out the base branch, for example
main. - Run
cargo bench -- --save-baseline main. - Check out your feature branch.
- Run
cargo bench -- --baseline main.
If you have uncommitted changes, git stash the changes, record the baseline, then git stash pop. Take both measurements in one sitting. Background load, CPU temperature, and power settings drift over hours, and that drift can look like a code change.
Gotchas
Bench filters and libtest. cargo bench with no target restriction also runs the default test harness for your library and binary targets. That harness does not know Criterion's flags, so you may see error: Unrecognized option: 'save-baseline'. Restrict the command to your Criterion target with --bench:
cargo bench --bench fib_bench -- --save-baseline beforeA missing baseline is an error. If you run --baseline before and before was never saved for some benchmark, Criterion stops with a message saying the baseline must exist first. This commonly happens when you add a new benchmark after saving a baseline. Recent Criterion versions provide --baseline-lenient, which compares where a baseline exists and skips the comparison where it does not. Check cargo bench -- --help for your version.
Baselines live in target/. cargo clean deletes them. They are also not meant to be committed. Saved data from one machine is not a valid reference on another, because CPU model, clock speed, and memory differ. If you need to keep a baseline, copy target/criterion somewhere safe or store it as a CI artifact, and compare only on the same hardware.
Overwriting is silent. Running --save-baseline before a second time replaces the earlier before data. Use descriptive, unique names such as main-2026-12-02, or a commit hash, if you want to keep several.
Different configuration breaks comparisons. If you change the sample size or measurement time between the two runs, the comparison is still computed, but the two distributions were gathered differently. Keep the configuration fixed while you compare.
Quick reference
| Goal | Command |
|---|---|
| Save results under a name | `cargo bench --bench NAME -- --save-baseline LABEL` |
| Compare a new run to a saved baseline | `cargo bench --bench NAME -- --baseline LABEL` |
| Compare two saved baselines without measuring | `cargo bench --bench NAME -- --load-baseline A --baseline B` |
| Change the noise threshold | `cargo bench --bench NAME -- --noise-threshold 0.03` |
| Change the significance level | `cargo bench --bench NAME -- --significance-level 0.01` |
Exercise
You saved a baseline with cargo bench -- --save-baseline before, edited your code, and ran cargo bench -- --baseline before. Criterion printed "Change within noise threshold." with a change interval of [+0.4% +0.8% +1.2%] and p = 0.01. Did the code get slower, and what could you change if you want Criterion to flag differences this small?
Show answer
The interval is entirely positive and p is below 0.05, so a small slowdown of roughly 0.8% probably exists. It is below the default noise threshold of 1%, so Criterion does not call it a regression. To flag differences this small, lower the noise threshold, for example with --noise-threshold 0.005 or .noise_threshold(0.005) in the configuration. On a noisy machine this raises the chance of false alarms.
Exercise
A teammate runs cargo bench -- --save-baseline before and gets error: Unrecognized option: 'save-baseline'. What is the cause, and what command fixes it?
Show answer
Plain cargo bench also runs the default libtest harness for library or binary targets, and that harness does not understand Criterion's flags. Limit the run to the Criterion target, for example cargo bench --bench fib_bench -- --save-baseline before.
Exercise
You saved baselines named main and feature in one session on the same machine. How can you compare them again tomorrow without re-measuring, and why does that command not depend on machine noise?
Show answer
Run cargo bench --bench fib_bench -- --load-baseline feature --baseline main. --load-baseline reads the saved feature data instead of sampling, and --baseline main supplies the reference. No new timing happens, so the result only depends on the stored data. The data must still exist in target/criterion, so do not run cargo clean in between.