On this page
cProfile.runctx
Prev: cProfile.run | Up: cProfile
Rel: cProfile.Profile, cProfile.run
Ref: docs.python.org — cProfile.runctx
Tags: #public #python3 #stdlib #cProfile
Import
import cProfile
from cProfile import runctx
Signature & Overview
cProfile.runctx(statement, globals, locals, filename=None, sort=-1)
Run statement under profiler, supplying your own globals and locals, optionally saving results in filename.
Usage
import cProfile
import contextlib
import io
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
cProfile.runctx("sum(range(n))", {}, {"n": 1000}, sort="cumtime")
assert "function calls" in buf.getvalue()
Gotchas
- Like
run(), with explicitglobalsandlocalsdicts: Handy inside functions when profiling code that references local variables.
REPL Inspect
from cProfile import runctx
help(runctx)
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/run 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