What one benchmark number cannot show#
A regression report from a single run compares one sample of the old code with one sample of the new. Nothing in that pair says how much the same code varies from run to run on that machine, so a 7% difference could be the change, the runner, or both.
benchstat, the Go project's comparison tool, is built around that missing information. Its documentation asks for repeated runs: "Each benchmark should be run at least 10 times to gather a statistically significant sample of results. For each benchmark, benchstat computes the median and the confidence interval for the median." It compares two sets of runs with "the Mann-Whitney U-test for A/B comparisons" and prints ~ when the difference could be chance. benchstat documentation, golang/perf at 4febdb082b2c
Identical code, measured twice#
The benchmark below is a constructed example: FNV-1a over a 4 KiB key. It ran under Go 1.24.7 on a four-vCPU Intel Xeon VM that other jobs were using at the same time (the load average read 8.4 just after these runs). benchstat came from golang.org/x/perf/cmd/benchstat@v0.0.0-20261117185222-4febdb082b2c, which the Go command built with Go 1.26.11 because that module requires it.
func Hash(key []byte) uint64 {
h := uint64(14695981039346656037)
for _, c := range key {
h ^= uint64(c)
h *= 1099511628211
}
return h
}
func BenchmarkHash(b *testing.B) {
for i := 0; i < b.N; i++ {
sink = Hash(key)
}
}
The test binary was compiled once and run twice with ten counts each, with no change in between:
$ go test -c -o ../old.test
$ ./old.test -test.run='^$' -test.bench=. -test.benchtime=200000x -test.count=10 > run1.txt
$ ./old.test -test.run='^$' -test.bench=. -test.benchtime=200000x -test.count=10 > run2.txt
$ benchstat run1.txt run2.txt
cpu: Intel(R) Xeon(R) Processor @ 2.10GHz
│ run1.txt │ run2.txt │
│ sec/op │ sec/op vs base │
Hash-4 6.359µ ± 20% 6.224µ ± 14% ~ (p=0.739 n=10)
The medians differ by 2%, and benchstat marks the difference ~. Its documentation defines the mark: the ~ "means benchstat did not detect a statistically significant difference between the two inputs." The confidence intervals, ±20% and ±14%, are the more useful number here. They say this machine cannot resolve a change of a few percent at ten counts.
The raw samples show what a one-shot job would have reported. Pairing each sample of run1.txt with the sample at the same position in run2.txt gives these differences for identical code, in percent, computed from the ns/op column with a short script:
-4.7 -10.4 -4.9 -11.0 +1.7 +20.2 -12.7 +0.6 +2.3 +1.1
Four of the ten pairs differ by more than 7% in one direction or the other, and one shows a 20% "regression". A job that runs each side once and alerts above a fixed percentage will raise findings like these with no change to explain them.
The disconfirmation: a small change that is real#
The change under review, also constructed, mixes a 256-byte salt into every hash before the key. It does 4,352 bytes of work instead of 4,096, so the expected cost is 6.25% more time per call, close to the size the job reported. The benchstat documentation says how to collect the samples: "The best way to do this is to interleave before and after runs, rather than running, say, 10 iterations of the before benchmark, and then 10 iterations of the after benchmark." Interleaving spreads a burst of load from another job across both sides instead of landing on one.
$ for i in $(seq 20); do
> ./old.test -test.run='^$' -test.bench=. -test.benchtime=200000x -test.count=1 >> old.txt
> ./new.test -test.run='^$' -test.bench=. -test.benchtime=200000x -test.count=1 >> new.txt
> done
$ benchstat old.txt new.txt
│ old.txt │ new.txt │
│ sec/op │ sec/op vs base │
Hash-4 5.987µ ± 1% 6.322µ ± 1% +5.60% (p=0.000 n=20)
This time the intervals are ±1% (the load average had dropped to 3.8), the medians differ by 5.60%, and the p-value rounds to zero with 20 samples on each side. The measured change is close to the 6.25% predicted from the extra bytes. This regression is real, it is smaller than the noise in any single pair of the earlier runs, and only the repeated, interleaved comparison shows it. The documentation makes the same point about size: "with enough low-noise data, even very small changes can be distinguished from noise and considered statistically significant."
What ~ does not tell you#
A ~ means the data cannot separate the two versions. It does not mean the versions perform the same. An earlier candidate for the same change folded ASCII upper case to lower case inside the loop, adding a comparison per byte. Compared the same interleaved way on the same VM, with the load average near 1.5, it gave:
Hash-4 5.978µ ± 2% 6.105µ ± 1% +2.12% (p=0.021 n=20)
A 2% cost is far smaller than the ±20% and ±14% intervals of the identical-code comparison above, so measurements that noisy could not have separated it from chance. The right record for a ~ result is "no change detected at ±N%", not "no regression". If a change of that size matters for the code, the next step is a quieter machine or more counts chosen in advance, as the documentation advises: "Pick a number of benchmark runs (at least 10, ideally 20) and stick to it."
Rerunning until a result appears is the opposite error. The same section warns: "If benchstat reports no statistically significant change, avoid simply rerunning your benchmarks until it reports a significant change." With the default threshold of 0.05, benchstat "is expected to show a difference 5% of the time even if there is no difference", and a suite of many benchmarks will show some significant results on every run for the same reason.
Decide from the comparison#
| What the finding says | Evidence to collect | Decision |
|---|---|---|
| One run before, one after, a difference above a threshold | 10 to 20 interleaved runs of each binary, compared with benchstat | Do not act on the single pair; collect the comparison first |
| benchstat shows a change with a small p-value and narrow intervals | Whether the size matches what the code change should cost | Treat it as real; fix it or record that the cost is accepted |
benchstat shows ~ with wide intervals | The interval width and the machine's load during the runs | Record "not detected at ±N%"; rerun on a quieter machine if that size matters |
benchstat shows ~ with narrow intervals | The count used and whether it was fixed in advance | Dismiss the regression report |
| Several benchmarks in a large suite each show a small significant change | How many benchmarks ran in total | Expect about 5% by chance; confirm any that matter with a fresh, planned run |
When a profiler, rather than a benchmark, names the slow code, the guide to profiler hotspots covers how the measurement itself can mislead. When a reviewer proposes a speed-up without a measurement, the guide to performance suggestions covers when it is worth taking.