Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Module cProfile

Profile a workload by recording call events, counts and elapsed times.

Version: Python 3.14; repository image, production runtime not attested

Syntax

cProfile.Profile().runcall(function, *args)

Profile a workload by recording call events, counts and elapsed times.

The profiler distinguishes time in a function itself from cumulative time including callees, and pstats summarizes the results. Profiling introduces overhead, so elapsed durations vary between runs.

The example checks call counts and the returned work result rather than printing unstable timings.

Examples

import cProfile
import pstats

def square(x):
    return x * x
def workload():
    return sum(square(i) for i in (1, 2, 3))
profile = cProfile.Profile()
print(profile.runcall(workload))
stats = pstats.Stats(profile)
print(next(row[1] for key, row in stats.stats.items() if key[2] == "square"))

Result

14
3

The example checks call counts and the returned work result rather than printing unstable timings.

Sources