Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / Python

Modul cProfile

Profilira kodo z beleženjem klicev, števila in časa.

Različica: Python 3.14; repository image, production runtime not attested

Sintaksa

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

Profilira kodo z beleženjem klicev, števila in časa.

Loči lastni čas od skupnega časa s podklici, podatke pa obdela pstats. Merjenje prinese režijo in trajanje niha.

Rezultat računa in trije klici square so stalni.

Primeri

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"))

Rezultat

14
3

Rezultat računa in trije klici square so stalni.

Viri