⚙️Profiling
Also known as Profiler, Code Profiling, Performance Profiling
Finding the parts of a program that take the most time.
Think of it like
Think of it like a stopwatch on each runner in a race. You see who slows the team down.
Example
Lucy's page takes 6 seconds to load. The profiler shows one function that resizes images takes 5 of them. She fixes just that one.
Why it matters
People are bad at guessing what is slow. A profile shows the truth in a few minutes.
Definition
Example code
console
$ python3 -m cProfile -s cumtime slow.py
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.500 0.500 slow.py:2(load)
1 0.000 0.000 0.100 0.100 slow.py:4(draw)Common mistake
Key takeaways
- 1Measure first. Then change the code.
- 2Time and memory. Per function.
- 3Find the big one. One part often costs most.