RC RANDOM CHAOS

Size a micro-benchmark so one run takes 300 milliseconds

matklad's rule for micro-benchmarks: tweak the input until one run takes about 300 milliseconds, keeping results readable and free of fixed-cost noise.

· 2 min read
Size a micro-benchmark so one run takes 300 milliseconds

matklad’s rule for sizing a micro-benchmark is to tweak the input until one run takes about 300 milliseconds. The target is deliberate, and the reasoning transfers to any benchmark you write.

Start with readability. At hundreds of milliseconds, every result is an integer between 1 and 999. That is enough precision to catch a small improvement and easy to scan by eye. You never juggle units or squint at a decimal point; comparing 1.31s with 239ms takes a beat, while a column of three-digit millisecond numbers reads at a glance.

Next, the noise floor. Below roughly 10ms, fixed costs start to dominate the measurement. Interpreter startup, process spin-up, a one-off allocation: those overheads skew a short run, and the usual remedy is to measure and subtract them, which adds machinery that is itself less reliable. At hundreds of milliseconds the per-run overhead becomes a rounding error, so you can ignore it without any special handling.

Third, human perception. Hundreds of milliseconds is fast but still noticeable, so keeping the number in that range lets you use your own sense of time instead of relying only on the digits. The payoff is concrete: watching a sluggish CLI command become “instant” after an optimization pass tells you something the raw figure alone does not.

Fourth, iteration speed. Past a second, the benchmark itself becomes the bottleneck. If you want to run it ten times in a row to eyeball the variance, each run has to be cheap, and 300ms lets you do that in a few seconds.

Underneath all four reasons sits one assumption that matklad states plainly: the purpose of this kind of benchmarking is to give the author enough intuition to make a correct decision about a change, not to produce a precise measurement of performance. Size the benchmark for the human reading it, and hundreds of milliseconds is where the number stays both readable and resistant to noise.

Share

Keep Reading

Latest on the Wire

Full wire →

New signal daily · RSS

Stay in the loop

New writing delivered when it's ready. No schedule, no spam.