KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / Python

Module cProfile

Profileert code door aanroepen, aantallen en tijden te registreren.

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

Syntax

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

Profileert code door aanroepen, aantallen en tijden te registreren.

Eigen tijd en cumulatieve tijd inclusief onderliggende aanroepen verschillen en pstats verwerkt de metingen. Profilering kost zelf tijd en de duur varieert.

Het werkresultaat en de drie aanroepen van square zijn stabiel.

Voorbeelden

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

Resultaat

14
3

Het werkresultaat en de drie aanroepen van square zijn stabiel.

Bronnen