Python — From Basics to Advanced · Performance and Profiling
cProfile, pstats and Sampling: py-spy, line_profiler
Performance and Profiling
Introduction
cProfile is Python's built-in function profiler. It measures how much time EACH function consumed during a program run. It answers: "where does the program spend 90% of its time?" — i.e. the hotspot worth optimising.
cProfile is a DETERMINISTIC profiler: it instruments every function call (hooked into sys.setprofile). It gives precise data but adds 1.5-3x overhead. The alternative is SAMPLING profilers like py-spy — they sample the stack every N ms, overhead under 5%, runnable in production.
This lesson covers: running cProfile in a script and via CLI, reading pstats output (cumtime vs tottime, ncalls), visualising with snakeviz and gprof2dot, when to reach for line_profiler (line-by-line) and py-spy (sampling, production).