The Function Holding Ninety Per Cent of the Time Does Nothing, and the One Doing the Work Holds Four Per Cent
Last timeHow Many Samples Is Enough
A profile has two time columns and they answer different questions. Reading the wrong one sends you to a function that is innocent or to one that cannot be fixed.
The two columns
Every profiler prints two numbers against each function and they are routinely
confused, so it is worth stating each one precisely.
Self time is the number of samples in which this function was the one actually
executing. Its own instructions, not anything it called.
Total time is the number of samples in which this function appeared anywhere on
the call stack, whether it was running or waiting for something it called to
return. Total time includes self time, and for a function that mostly calls
other things it is much larger.
| self time, per cent | total time, per cent | |
|---|---|---|
| handle_request | 1 | 94 |
| process_batch | 2 | 91 |
| transform_record | 4 | 88 |
| normalise_text | 71 | 71 |
| write_output | 12 | 12 |
| read_config | 8 | 9 |
When self time is the answer
The self column answers one question: which function is spending the most time
executing its own code. When the fix is to rewrite some code, this is the
column that finds it.
The condition to check alongside a high self time is the call count. A function
with seventy per cent of self time and a thousand calls is genuinely expensive
per call, and the loop inside it is worth examining. The same seventy per cent
over fifty million calls is a different finding entirely, which brings us to
the other column.
The lesson stops here
4 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