On this page
cProfile
Prev: bz2 | Up: stdlib_module | Next: calendar
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — cProfile
Tags: #public #python3 #stdlib
Import
import cProfile
Overview
Deterministic C-extension CPU profiler (Profile, run, python -m cProfile) that records exact call counts, self time (tottime), and cumulative time (cumtime) for every Python function with low overhead. Always prefer cProfile over the pure-Python profile module, and inspect saved profiles with pstats.
Quick Start
import cProfile
import pstats
with cProfile.Profile() as pr:
_ = sum(i * i for i in range(50_000))
stats = pstats.Stats(pr).sort_stats("cumtime")
assert stats.total_calls > 0
Implementation Walkthrough
1. Profiling a Script from the Command Line & Saving .prof Output
# Print top 20 functions by cumulative time:
python3 -m cProfile -s cumtime my_script.py | head -n 30
# Save binary profile for interactive inspection with pstats or snakeviz:
python3 -m cProfile -o run.prof my_script.py
Gotchas & Boundaries
- Measures wall/CPU time inside Python calls, not line-by-line cost: For tiny micro-benchmarks use timeit; for memory use tracemalloc.
- Watch
tottimevscumtime:tottimeexcludes time spent in sub-calls (shows hot leaf functions);cumtimeincludes sub-calls (shows hot entry points). - Single-thread by default: Only profiles the thread that enabled it (unless each worker thread runs its own profiler).
Common Functions
- cProfile.run() —
run(statement, filename=None, sort=-1)— Run statement under profiler optionally saving results in filename This function takes a single argument that can be passed to the "exec" statement, and an optional file name. In all cases this - cProfile.runctx() —
runctx(statement, globals, locals, filename=None, sort=-1)— Run statement under profiler, supplying your own globals and locals, optionally saving results in filename.
Classes & Objects
- cProfile.Profile — Builds a profiler object using the specified timer function. (Methods:
.clear(),.create_stats(),.disable(),.dump_stats(),.enable(),.getstats())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/bz2
- tuts/python3/stdlib_module/calendar
- tuts/python3/stdlib_module/cProfile/Profile
- tuts/python3/stdlib_module/cProfile/run
- tuts/python3/stdlib_module/cProfile/runctx
- tuts/python3/stdlib_module/profile
- tuts/python3/stdlib_module/pstats
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/intermediate_python_and_software_engineering/testing_and_debugging
Related notes
- tuts/python3/stdlib_module/bz2 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/calendar Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cProfile/Profile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cProfile/run Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cProfile/runctx Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pstats Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/doctest Linked here; shared tags: #stdlib