Member-only story
Stop Using cProfile in 2025 — Better Ways to Find Python Bottlenecks
The CPython core team keeps tuning the interpreter: Python 3.13 (October 2024) shipped a public sys.monitoring
API that tools can hook into without monkey-patching . Paradoxically, the built-in cProfile still writes its data in a 2006-era format called pstat. The file is small, but what it stores is so limited that many visualizers display misleading pictures, and heavy programs slow down markedly while being profiled.
Where cProfile Falls Short
1. Heavy overhead
Running python -m cProfile your_app.py
can tack tens of percent onto runtime—painful if you are chasing a 10 ms hiccup.
2. Thin metadata
pstat records only:
- number of calls (with and without recursion)
- “self” time inside the function
- cumulative time (self + callees)
It drops thread IDs, async task IDs, process IDs — everything modern back-ends rely on.
3. Lost call-chain context
Imagine three endpoints — /login
, /search
, /export
—all calling the helper query_db()
, which in turn invokes decode_row()
. In pstat you see just one lump sum for…