Numerical practice · timing
Benchmarking Numerical Code: Methods and Mistakes
A benchmark answers one question: how long does this computation take, on this machine, under these conditions? Most bad benchmarks fail by answering a different question. They time one cold run, measure the wrong thing, or compare two tools that were silently doing different work. This page sets out a method that avoids the usual traps, and it contains no measured results on purpose: numbers from someone else’s hardware tell you little about yours.
Decide what is being measured
Start with the question. Is it the speed of one library routine, the speed of a whole program, or the scaling of an algorithm as the problem grows? Each needs a different design. A routine benchmark isolates the call and fixes the inputs. A program benchmark includes input and output and everything around the kernel. A scaling study varies the problem size and looks at the trend.
State the work being done. Two programs that both claim to solve a linear system may differ in precision, in whether they check for singularity, or in whether one factors the matrix and the other uses an iterative method. Compare only after confirming that the answers agree to the accuracy that matters, using the residual of Ax − b rather than the eyeball.
Warm-up, repetition and the clock
The first execution of a function is not representative. Code gets loaded, caches fill, a just-in-time compiler may translate the function, and libraries start worker threads. Run the computation once or twice and discard those timings.
Then repeat. A single measurement is a sample of a noisy process: other programs wake up, the processor changes its clock, and the operating system interrupts you. Take many repetitions, at least ten for fast calls, and report the median or the minimum rather than the mean. The mean is dragged up by occasional disturbances that have nothing to do with your code. Giving the spread, such as the interquartile range, tells the reader how much to trust the number.
Use a wall-clock timer with a monotonic source, and make sure each measured run lasts long enough, ideally tens of milliseconds or more, that the timer resolution is irrelevant. For very short operations, time a batch of calls in a loop and divide.
repeat 3 times: run(f, input) # warm-up, discarded
times = []
repeat 20 times:
t0 = clock()
result = f(input)
t1 = clock()
times.append(t1 - t0)
report median(times), min(times), spread(times)
Fix the inputs, and keep the compiler from cheating
Use inputs of realistic structure. Random dense matrices make a fair test for dense linear algebra, but a sorting routine or a sparse solver depends heavily on the data. Seed the random generator so that every run and every tool sees the same input, and generate it outside the timed region.
Make sure the result is used. An optimizing compiler may remove a computation whose output is never read, and then the timer records nothing. Consume the result, for instance by summing it or comparing it against a reference, after the clock has stopped.
Watch for caching of results inside the tool itself, and for lazy evaluation, where a call returns before the work is done. Accelerator devices behave this way, which is why a device synchronization must precede the stop of the clock; see GPU computing for matrix code.
Report rates, not just seconds
A time depends on problem size, so a rate is easier to compare. For dense matrix multiplication of two n × n matrices, the operation count is 2n³. For a 2000 × 2000 product that is 2 · 2000³ = 1.6 × 10¹⁰ operations, and the achieved rate is 1.6 × 10¹⁰ divided by the measured seconds. An LU factorization costs about 2n³/3, as detailed in LU factorization, and a solve with the factors adds about 2n² per right-hand side.
Comparing the achieved rate with the processor’s theoretical peak says how well the code uses the hardware. A routine at a few percent of peak has room to improve, and one near peak is done. The comparison also reveals memory-bound code, whose rate stays far below peak regardless of effort.
Vary the size and read the slope
Time the computation at several sizes, doubling n each time, and plot run time against n on logarithmic axes. An O(n³) algorithm shows a line of slope 3 asymptotically. Deviations are informative: a kink where the matrix stops fitting in cache, a plateau at small sizes where overhead dominates, a jump when memory starts swapping. A single size can hide all of this, and the ranking of two methods may flip as n grows.
Control the environment
Record the processor model, core count, memory, operating system, language version and the linear algebra library with its thread count. Library threading matters a great deal for linear algebra: a routine running on eight threads versus one differs by a large factor, and a comparison between tools is meaningless if one used all cores and the other did not. Set the thread count explicitly, and state it.
Close other heavy programs, and disable power-saving modes if the machine allows it. On laptops, thermal throttling makes later repetitions slower than earlier ones; a long benchmark should alternate the competing tools rather than run all of one after the other.
Honest conclusions
Report what you measured, with its conditions, and nothing more. A result such as “A was faster than B in this test with these settings” is a finding. A claim that A is faster is a guess about every other situation. When the difference is small compared with the spread, say that the two are indistinguishable.
Differences between interpreted loops and array expressions are a common reason that benchmarks of whole languages disagree; the background is in what a matrix language is and MATLAB-style matrix languages compared. The linear algebra hub links the methods that are worth timing.