Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / Python

Modul cProfile

Profiluje kód zaznamenávaním volaní, ich počtu a času.

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

Syntax

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

Profiluje kód zaznamenávaním volaní, ich počtu a času.

Rozlišuje vlastný čas a súhrnný čas s vnorenými volaniami, údaje spracuje pstats. Profilovanie pridáva réžiu a časy kolíšu.

Výsledok výpočtu a tri volania square sú stabilné.

Prí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ýsledok

14
3

Výsledok výpočtu a tri volania square sú stabilné.

Zdroje