Python Glossary Русский

Python 3.9+pstatsterm

pstats.FunctionProfile

programmatic access

A dataclass with the stats of one function from a profile: ncalls, tottime, cumtime, per-call times, file and line.

Syntax

fp = stats.get_stats_profile().func_profiles['имя']

You get it from Stats.get_stats_profile().func_profiles rather than creating it. Trap: ncalls is a string, not a number, because recursion shows it as '177/1'; ncalls > 100 raises TypeError and int('177/1') raises ValueError. The numbers live in tottime and cumtime (seconds, float); file_name and line_number say where the function is defined.

Examples

import cProfile, pstats
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)
with cProfile.Profile() as pr:
    fib(10)
fp = pstats.Stats(pr).get_stats_profile().func_profiles['fib']
print(type(fp).__name__)  # → FunctionProfile
print(repr(fp.ncalls))  # → '177/1'
print(isinstance(fp.cumtime, float))  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy