all Python 3pstatsterm
pstats.Stats
profile report
Loads profiler results from a Profile or files and prints a report: sorting, filtering, callers and callees.
Syntax
pstats.Stats(*filenames_or_profile, stream=sys.stdout)
Methods modify the object in place and return it, so calls are chained: strip_dirs().sort_stats('cumulative').print_stats(10). An int in print_stats is how many rows to show, a float between 0 and 1 a fraction, a string a regular expression on the name. The report goes to stream (sys.stdout by default) rather than being returned — pass io.StringIO() to get text. Profile files are not portable between Python versions.
Examples
import cProfile, pstats, io
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
with cProfile.Profile() as pr:
fib(10)
out = io.StringIO()
st = pstats.Stats(pr, stream=out)
print(st.strip_dirs().sort_stats('cumulative') is st) # → True
st.print_stats(3)
print('function calls' in out.getvalue()) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy