CKTIS / Python
Modul cProfile
Profiluje kód zaznamenáváním volání, jejich počtu a času.
Verze: Python 3.14; repository image, production runtime not attested
Syntaxe
cProfile.Profile().runcall(function, *args)Profiluje kód zaznamenáváním volání, jejich počtu a času.
Rozlišuje vlastní čas a souhrnný čas s vnořenými voláními, údaje zpracuje pstats. Profilování přidává režii a časy kolísají.
Výsledek výpočtu a tři volání square jsou stabilní.
Příklady
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"))
Výsledek
14 3
Výsledek výpočtu a tři volání square jsou stabilní.