Last Updated: June 6, 2026
Profiling answers a single question: where is the program spending its time? Without that answer, optimizing Python code is guesswork, and most guesses are wrong. A function that seemed like the bottleneck may take five percent of the runtime, while a forgotten loop in a helper takes eighty. This lesson covers cProfile (the built-in deterministic profiler), pstats for reading its output, line-level profiling with line_profiler, statistical profilers like py-spy and scalene, and the workflow that ties them together.