On this page
cProfile.Profile
Up: cProfile | Next: cProfile.run
Rel: cProfile.run, cProfile.runctx
Ref: docs.python.org — cProfile.Profile
Tags: #public #python3 #stdlib #cProfile
Import
import cProfile
from cProfile import Profile
Signature & Overview
cProfile.Profile(timer=None, timeunit=0.0, subcalls=True, builtins=True)
Builds a profiler object using the specified timer function.
Methods & Attributes
Profile.clear(self, /)— Clear all profiling information collected so far.Profile.create_stats(self)—Profile.create_statsmethod/property.Profile.disable(self, /)— Stop collecting profiling information.Profile.dump_stats(self, file)—Profile.dump_statsmethod/property.Profile.enable(self, /, subcalls=True, builtins=True)— Start collecting profiling information.Profile.getstats(self, /)— list of profiler_entry objects.Profile.print_stats(self, sort=-1)—Profile.print_statsmethod/property.Profile.run(self, cmd)—Profile.runmethod/property.Profile.runcall(self, func, /, *args, **kw)—Profile.runcallmethod/property.Profile.runctx(self, cmd, globals, locals)—Profile.runctxmethod/property.Profile.snapshot_stats(self)—Profile.snapshot_statsmethod/property.
Usage
import cProfile
import io
import pstats
with cProfile.Profile() as pr:
_ = sum(i * i for i in range(2000))
buf = io.StringIO()
pstats.Stats(pr, stream=buf).sort_stats("tottime").print_stats(5)
assert "function calls" in buf.getvalue()
Gotchas
- Supports
with cProfile.Profile() as pr:: Cleanest way to profile a specific block without instrumenting the whole program.
REPL Inspect
from cProfile import Profile
help(Profile)
Linked from
Related notes
- tuts/python3/stdlib_module/cProfile/run 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