Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / Python

Módulo cProfile

Perfila el código registrando llamadas, recuentos y tiempos.

Versión: Python 3.14; repository image, production runtime not attested

Sintaxis

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

Perfila el código registrando llamadas, recuentos y tiempos.

Distingue tiempo propio y acumulado con subllamadas, y pstats procesa los datos. El perfilado añade coste y las duraciones varían.

El resultado del trabajo y las tres llamadas a square son estables.

Ejemplos

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

Resultado

14
3

El resultado del trabajo y las tres llamadas a square son estables.

Fuentes