Python 3.7+pstatsterm
pstats.SortKey
profile report
An enumeration of sort keys for Stats.sort_stats(): CALLS, CUMULATIVE, TIME, NAME and others.
Syntax
stats.sort_stats(SortKey.CUMULATIVE, SortKey.NAME)
A typo in a string ('cumtme') surfaces only at call time as KeyError, while SortKey.CUMTIME fails at once with AttributeError and editor help. Strings also accept unambiguous prefixes ('cumu', 'tot') but not 'cum', which matches both cumulative and cumtime. TIME means tottime, the time inside the function itself excluding sub-calls; CUMULATIVE includes them. Members subclass str and equal their strings.
Examples
from pstats import SortKey
print(SortKey.CUMULATIVE == 'cumulative') # → True
print(SortKey.TIME.value) # → time
import cProfile, pstats, io
with cProfile.Profile() as pr:
sorted(range(100))
st = pstats.Stats(pr, stream=io.StringIO())
try:
st.sort_stats('cumtme')
except KeyError as e:
print(e) # → 'cumtme'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy