Profiling and Memory Modules

pstats

Reads and organizes deterministic profiler statistics. Sort by cumulative time to investigate expensive call paths or internal time to find costly function bodies. Stats can combine compatible profiles, but timing values depend on workload and environment.

Formal Syntax & Structure

python syntaxpython
import pstats

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import cProfile, pstatsfrom io import StringIOprofiler = cProfile.Profile()profiler.runcall(sum, [1, 2, 3])stats = pstats.Stats(profiler, stream=StringIO())print(stats.total_calls > 0)
Python Reference