On this page
pstats
Prev: profile | Up: stdlib_module | Next: pty
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — pstats
Tags: #public #python3 #stdlib
Import
import pstats
Overview
Loads, filters, merges, and sorts binary profile dumps produced by cProfile (Stats, SortKey, strip_dirs, print_stats, print_callers, print_callees). Essential for turning raw .prof files into actionable bottleneck reports.
Quick Start
import cProfile
import io
import pstats
with cProfile.Profile() as pr:
_ = sorted(range(10_000, 0, -1))
out = io.StringIO()
pstats.Stats(pr, stream=out).strip_dirs().sort_stats(pstats.SortKey.CUMULATIVE).print_stats(5)
assert "function calls" in out.getvalue()
Implementation Walkthrough
1. Filtering & Inspecting Callers / Callees
import pstats
def top_report(prof_path: str, limit: int = 15) -> None:
stats = pstats.Stats(prof_path)
stats.strip_dirs()
stats.sort_stats(pstats.SortKey.TIME)
stats.print_stats(limit)
stats.print_callers(5)
Gotchas & Boundaries
- Call
.strip_dirs()before.print_stats()to make output readable by removing long absolute filesystem prefixes. - Pass
stream=io.StringIO()topstats.Stats(pr, stream=buf)when capturing profiler output in tests or logs instead of printing tostdout. - Combine multiple runs with
stats.add("run2.prof")to aggregate across workloads.
Classes & Objects
- pstats.FunctionProfile — FunctionProfile(ncalls: str, tottime: float, percall_tottime: float, cumtime: float, percall_cumtime: float, file_name: str, line_number: int)
- pstats.SortKey — An enumeration. (Methods:
.capitalize(),.casefold(),.center(),.count(),.encode(),.endswith()) - pstats.Stats — This class is used for creating reports from data generated by the Profile class. It is a "friend" of that class, and imports data either by direct access to members of Profile class, or by reading in a dictionary (Methods:
.add(),.calc_callees(),.dump_stats(),.eval_print_amount(),.get_print_list(),.get_sort_arg_defs()) - pstats.StatsProfile — Class for keeping track of an item in inventory.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cProfile
- tuts/python3/stdlib_module/profile
- tuts/python3/stdlib_module/pstats/FunctionProfile
- tuts/python3/stdlib_module/pstats/SortKey
- tuts/python3/stdlib_module/pstats/Stats
- tuts/python3/stdlib_module/pstats/StatsProfile
- tuts/python3/stdlib_module/pty
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/cProfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/profile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pstats/FunctionProfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pstats/SortKey Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pstats/Stats Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pstats/StatsProfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pty Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib Linked here; shared tags: #stdlib