Sub topic 11
Saving and Comparing Baselines
This chapter assumes you can run cargo bench on a Criterion benchmark and read its console output. You should know what bench_function and black_box do. If not, review the earlier chapters on writing a first benchmark and reading the output.
Why a baseline
A single benchmark number says little by itself. You usually want to know whether a code change made things faster or slower. A baseline is a saved set of measurements from an earlier run. Criterion compares a new run against it and reports the difference.
Criterion already does this on its own. After each run it stores the results under target/criterion. The next cargo bench compares against the previous run and prints a change estimate. That default is handy, but it has a weakness. Every run overwrites the reference. If you run the benchmark twice after an edit, the second run compares against the first, not against the code you started from.
Named baselines fix this. You choose when the reference is saved, and you choose which reference to compare against.
Saving and comparing
Two flags control named baselines. Both go after --, which passes them to Criterion instead of Cargo.
--save-baseline NAMEruns the benchmarks and stores the results underNAME.--baseline NAMEruns the benchmarks and compares them with the results stored underNAME.
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 records the current code as before. After you edit the code, the third command measures again and reports the change relative to before.
Reading the comparison
The comparison output adds a change line and a verdict to each benchmark. For the fib 20 benchmark it looks like this:
fib 20 time: [24.1 µs 24.3 µs 24.6 µs]
change: [-12.4% -11.8% -11.1%] (p = 0.00 < 0.05)
Performance has improved.The change line gives a confidence interval for the relative difference in time. A negative number means the new code is faster. The p-value comes from a hypothesis test. By default Criterion uses a significance level of 0.05. If p is below it, Criterion reports a real change. Otherwise it reports No change in performance detected, or Change within noise threshold when the shift is small.
The noise threshold, 1% by default, is a second filter. A change can be statistically significant and still be smaller than the threshold. Criterion then treats it as noise. This matters on shared machines, where a 1% shift is rarely meaningful.
Practical habits
Follow these rules to keep comparisons honest.
- Save the baseline before you edit. A baseline saved after the change measures the wrong code.
- Use the same machine and conditions for both runs. Close other heavy programs and keep power settings the same.
- Give baselines descriptive names, such as
mainorbefore-inline, so you can tell them apart later. - Keep the benchmark names stable. Criterion matches results by benchmark name. If you rename a benchmark, it has nothing to compare against.
- Run
cargo benchwithout flags only when you want the rolling comparison with the last run.
Checking a change end to end
Suppose you want to see whether a rewrite of fibonacci helps. The steps are:
- On the original code, run
cargo bench -- --save-baseline before. - Rewrite the function.
- Run
cargo bench -- --baseline before. - Read the
changeline for each benchmark. Confirm the p-value and that the interval is clear of zero. - Check the HTML report in
target/criterion/report/index.html. It plots the old and new distributions together.
If the report says performance regressed, you have evidence tied to a specific saved run, not a guess.
Exercise
You ran cargo bench -- --save-baseline before, edited your code, and ran cargo bench -- --baseline before. Criterion prints change: [-0.4% +0.1% +0.7%] (p = 0.62 > 0.05). What should you conclude, and why?
Show answer
Conclude that there is no detectable change in performance. The interval spans zero, and the p-value of 0.62 is above the 0.05 significance level, so the difference is consistent with noise. You should not claim the edit made the code faster or slower.
Exercise
A teammate saves a baseline named main, renames the benchmark from fib 20 to fibonacci 20, and then compares against main. What goes wrong?
Show answer
Criterion matches results by benchmark name. The renamed benchmark has no entry in the main baseline, so there is nothing to compare against and no change report is produced for it.