Basis Lab

Reading a Profile Honestly

The New Version Is Eight Per Cent Faster, and So Is the Old One If You Run It Enough Times

Last timeFour Ways a Profile Misleads

A single before and after pair proves nothing, because the same program run twice differs by more than most improvements. Here is the comparison that survives that, and how to report it.

The same program, twice

Before any comparison, measure the thing that decides whether a comparison is possible: run the unchanged program ten times and look at the spread.

Most people are surprised. The numbers are not the same, and they are often not close. Several per cent between the fastest and slowest run of identical code on an idle machine is ordinary, and on a shared or virtualised machine it can be far worse.

FIG 1Where the variation comes from, with typical contributions
typical swing, per centcan be reducedcan be eliminatedshows up in a single pair
where the code and data landed in memory4101
cache and branch predictor state at start3101
other processes on the machine2011
processor frequency and temperature8000
which machine in the fleet3001
the order the runs happened in1101
The two marked rows are the largest and the two most often ignored. Memory layout is set by the loader and the allocator and changes between runs for reasons that have nothing to do with your code. Frequency and temperature drift over minutes, which makes them the reason the second half of a session is slower than the first.

The memory layout figure deserves a moment, because it is the one that feels wrong. Where functions and data happen to land determines which of them share cache sets and which share pages, and that can move run time by several per cent with the instructions entirely unchanged. Address randomisation guarantees it differs between runs. This means a single measurement of a single build is partly a measurement of one accidental layout.

The consequence is the whole lesson. If the same program varies by four per cent, a single before and after pair showing a five per cent improvement is almost no evidence at all.

Running them alternately

The second problem is worse than noise, because averaging does not fix it.

Suppose you run the old version twenty times, then the new version twenty times. During those forty runs the machine warmed up, a background update started, and the processor dropped a frequency step. All of that lands on the second half, which is entirely the new version. The comparison now contains a real difference between the versions plus an unknown difference between the first and second halves of the afternoon, and nothing in the numbers separates them.

The lesson stops here

6 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01It Is Slow Is Not a Measurement, and Profiling Before You Have One Wastes the Week
  2. 02One Profiler Interrupts a Thousand Times a Second and Guesses, the Other Watches Every Call and Changes the Answeropening only
  3. 03The Function at Number Seven Received Nine Samples, Which Means You Know Almost Nothing About Itopening only
  4. 04The Function Holding Ninety Per Cent of the Time Does Nothing, and the One Doing the Work Holds Four Per Centopening only
  5. 05The Same Function Is Cheap in Two Places and Ruinous in the Third, and the Flat List Shows One Numberopening only
  6. 06Three and a Half of the Four Seconds Are Invisible, Because the Profiler Only Looks When the Program Is Runningopening only
  7. 07The Sample Landed Three Instructions After the One That Was Slow, and the Function It Blames Does Not Exist Any Moreopening only
  8. 08The New Version Is Eight Per Cent Faster, and So Is the Old One If You Run It Enough Timesyou are here

Read alongside