One Profiler Interrupts a Thousand Times a Second and Guesses, the Other Watches Every Call and Changes the Answer
Last timeAsking the Right Question First
The two ways of measuring a program are a statistical sample and a complete count. They have different costs, different resolutions and different distortions, and the question decides which.
How sampling works
A sampling profiler does something simpler than most people expect. At a fixed
rate, commonly a thousand times a second, it interrupts the program, looks at
where it is, writes down the call stack, and lets it continue.
That is the entire mechanism. The output is a count of how many times each
stack was observed. A function that was on the stack for a third of the run
appears in about a third of the samples, and the share of samples converges on
the share of time as the samples accumulate.
| step | sample | where it was | running total for the slow function | share so far | what happened |
|---|---|---|---|---|---|
| 1 | 1 | inside the slow function | 1 | 100 per cent | One sample and the profile says this function is the entire program. Early samples say nothing; the number only means something once there are many. |
| 2 | 120 | inside the slow function | 48 | 40 per cent | A hundred and twenty samples in, the estimate has steadied around forty per cent. The uncertainty here is still several percentage points, which is the subject of the next lesson. |
| 3 | 3000 | somewhere else | 1047 | 34.9 per cent | Three thousand samples, three seconds of running at a thousand a second. The estimate is now good to a fraction of a per cent for functions holding a large share. |
| 4 | 3000 | a function seen twice | 2 | 0.07 per cent | The same run says almost nothing about a function seen twice. Two samples could as easily have been zero or five, so any statement about this function is noise. Resolution depends on the share, not on the length of the run alone. |
Two properties follow from the mechanism and are worth stating because they
explain most of the behaviour of these tools.
The cost does not depend on the program. A thousand interrupts a second cost
the same whether the program is calling a million functions a second or sitting
in one loop, which is why sampling overhead is typically under two per cent and
does not change as the program changes.
And nothing is recorded between samples. The profiler has no idea how many
times anything was called, how long any individual call took, or that a
function existed at all if it never happened to be running at an interrupt.
Sampling answers where the time went and refuses every other question.
How counting works
The other method writes the measurement into the program. At every function
entry a record is made, at every exit another, and the difference is that
function call.
The lesson stops here
3 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 contentsThis 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