-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiling_cProfile.py
41 lines (31 loc) · 1.24 KB
/
profiling_cProfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import cProfile as profile
import pstats
def profiling_cProfile():
n = 10000000
lst = list(range(0, n))
x = 2
v = lst.copy()
for i in range(len(lst)):
v[i] = lst[i] * x
# Ejecutamos el programa y realizamos el profiling
prof = profile.Profile()
# Inicializamos la lista de tiempos
tiempos = []
# Ejecutamos el programa 5 veces y tomamos los tiempos
num_repeticiones = 5
for _ in range(num_repeticiones): #el uso de _ es como una convención para indicar que el valor del bucle no se va a usar en el cuerpo del bucle
prof.enable()
profiling_cProfile()
prof.disable()
stats = pstats.Stats(prof).strip_dirs().sort_stats("cumtime")
# Obtenemos el tiempo de ejecución de la última llamada
tiempo_ejecucion = stats.total_tt
tiempos.append(tiempo_ejecucion)
# Obtenemos el tiempo máximo y mínimo
tiempo_maximo = max(tiempos)
tiempo_minimo = min(tiempos)
# Eliminamos los tiempos max y min de la lista
tiempos_sin_max_min = [t for t in tiempos if t != tiempo_maximo and t != tiempo_minimo]
# Calculamos el tiempo promedio con los tiempos restantes
tiempo_promedio = sum(tiempos_sin_max_min) / len(tiempos_sin_max_min)
print("Tiempo promedio de ejecución:", tiempo_promedio)