Profiling a program
Measuring before changing anything: timing a snippet, choosing between an instrumenting and a sampling profiler, reading own time and cumulative time, a flame graph, and avoiding misleading measurements.
- 22 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Profiling: what is it?
Show the answer
Measuring, during a real run, the time or memory used by each part of a program.
Intuition is often wrong about what is expensive: measure first, decide after. Without a measurement you optimise a line that took 1% of the time.
In this
cProfilereport, which function should be optimised first?
```text
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.013 0.013 0.013 0.013 p.py:1(lent)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.013 0.013 p.py:7(main)
50 0.000 0.000 0.000 0.000 p.py:6(rapide)
1 0.000 0.000 0.013 0.013 {built-in method builtins.exec}
1 0.000 0.000 0.013 0.013 p.py:1(<module>)`lentmainrapide<module>
Show the answer
lentlenthas the biggesttottime: time is spent in its own body.mainand<module>have the samecumtime, but only because they calllent.rapideis called 50 times and still costs almost nothing.To time a snippet, repeat it several times and keep the … or the median of the times, since machine noise can only slow a measurement down.
Show the answer
To time a snippet, repeat it several times and keep the minimum or the median of the times, since machine noise can only slow a measurement down.
The mean is pulled up by a run disturbed by another process.
timeit.repeatreturns the list of each series' times: keep theirmin.Profiler: what is it?
Show the answer
A tool that runs a program while observing how much time (or memory) each function uses and how many times it is called.
There are two families: instrumenting (every call is recorded, like
cProfile, precise but slower) and sampling (the stack is looked at at regular intervals, light enough for production).The first run of a function takes 900 ms, the following ones 40 ms. A colleague concludes the function takes 900 ms. What do you answer?
- The 900 ms is the true value: later runs are skewed by the cache and do not count
- Average the two measured values, which gives about 470 ms
- Recompile the whole program before each of the measurements taken
- Cold start: repeat and keep the median after warm-up
Show the answer
Cold start: repeat and keep the median after warm-up
The first run loads code and data and fills the caches. Depending on the question, you measure cold start or steady state, never a mix of the two.