On this page
cProfile.run
Prev: cProfile.Profile | Up: cProfile | Next: cProfile.runctx
Rel: cProfile.Profile, cProfile.runctx
Ref: docs.python.org — cProfile.run
Tags: #public #python3 #stdlib #cProfile
Import
import cProfile
from cProfile import run
Signature & Overview
cProfile.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
Usage
import cProfile
import contextlib
import io
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
cProfile.run("sum(range(1000))", sort="tottime")
assert "function calls" in buf.getvalue()
Gotchas
- Evaluates code in
__main__namespace: Passfilename="out.prof"to save stats forpstats.Stats("out.prof")or visualization tools likesnakeviz.
REPL Inspect
from cProfile import run
help(run)
Linked from
Related notes
- tuts/python3/stdlib_module/cProfile/Profile Linked here; links to this page; shared tags: #cprofile, #stdlib
- tuts/python3/stdlib_module/cProfile/runctx Linked here; links to this page; shared tags: #cprofile, #stdlib
- tuts/python3/stdlib_module/cProfile Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib
- tuts/c/stdlib/ctype/isdigit shared tags: #stdlib