Skip to content

Lesson 2 of 13 · 13m

Benchmarks That Don't Lie

Warmup, repetition, and `perf stat` counters turn a `time` run into a number worth acting on.

time ./new-version run once against time ./old-version run once proves almost nothing, and yet that pair of numbers ships more regressions than any profiler. The CPU is not a metronome. Turbo boost raises and drops clocks with temperature and core count. SMT siblings share execution ports. The second run reads from a warm page cache the first run paid for. The scheduler migrates the process between cores mid-measurement. One number out of that soup is not a benchmark; it is an anecdote.

What a real measurement looks like

hyperfine (sharkdp's Rust tool) exists because this problem is already solved. It runs a warmup, repeats each command at least ten times, and reports the mean plus standard deviation:

hyperfine --warmup 3 'python old.py input.txt' 'python new.py input.txt'

If the stddev is a third of the mean, the measurement is noise, and the remedy is more runs on a quieter machine — not more faith. Pin the process: taskset -c 3 keeps the kernel from bouncing it across cores. Compare like with like — same input, same working directory, same ulimits, ideally an otherwise idle box. When the numbers matter, dedicate a core with isolcpus and boot-time housekeeping so nothing else schedules there.

The JIT trap

Managed runtimes add a second trap. A JVM benchmark that finishes in milliseconds never left the interpreter or the C1 compiler; tiered compilation promotes hot code to C2 at roughly ten thousand invocations. Worse, a loop whose result is never consumed is dead code — the JIT is within its rights to delete it entirely, and your optimized benchmark measures an empty loop.

JMH exists for exactly this: warmup forks, Blackhole.consume() to keep results alive, batched @Measurement rounds. If a JVM number looks too good to be true, it probably measured nothing. The same rule applies anywhere an optimizer can see the whole loop — consume or return the result, or the result is fiction.

The environment leaks into the number

Check the machine before trusting the delta. lscpu reports current CPU MHz — if it wobbles between runs, frequency scaling is in your data. /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor shows whether the governor is performance or powersave, and on cloud VMs the governor may be fixed while the burst budget stays invisible. NUMA matters too: a process bouncing across sockets pays remote-memory latency, and numactl --hardware shows the topology before you blame the code. Long-running servers reach page-fault and branch-predictor warmth that a cold time run never sees — the other reason production replays beat lab runs. And record the environment with the result: compiler flags, ulimits, kernel version, exact input file. A benchmark you cannot reproduce is a rumor.

Explaining the number

perf stat turns a timed run into an explained run:

perf stat -e task-clock,cycles,instructions,branches,branch-misses,cache-misses ./prog

Instructions-per-cycle under ~1 on a core that retires up to four wide means the pipeline stalled — usually on memory — and the difference between two builds shows up in branch-misses and cache-misses even when wall time is close. IPC is often a better regression tripwire than seconds, because seconds are what noise eats first.

The practice

  • Warmup runs, ten-plus measured runs, mean and stddev — hyperfine does all three for free.
  • Pin the CPU, quiet the machine, keep inputs identical.
  • Keep results alive — Blackhole.consume, returned values, printed checksums — so the optimizer cannot cheat.
  • Use perf stat deltas (IPC, cache-misses, branch-misses) to catch regressions wall time hides.
  • If the improvement is under ~5%, assume you need more runs or a bigger input before believing it. Sub-noise diffs get shipped as wins every week.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.