You are on page 1of 2

#%%

%reset -f
import numpy as np
from math import log10 as lg10
import time
import matplotlib.pyplot as plt

# Numero de elementos a procesar


N = 1000000

# Lista para almacenar los tiempos de procesamiento


speed = []

# Dato de entrada
l1 = list(100*(np.random.random(N))+1)
len(l1)

#
===================================================================
==========
# Usando for
#
===================================================================
==========
l2 = []
# Usar for + append
t1 = time.time()
for item in l1:
l2.append(lg10(item))
t2 = time.time()
print("Tiempo usado por el bucle for {} segundos".format(t2-t1))
speed.append(t2-t1)
#
===================================================================
==========
# Usando comprehension de listas
#
===================================================================
==========
t1 = time.time()
l2 = [lg10(i) for i in range(1,1000001)]
t2 = time.time()
print("Tiempo usando list comprehensions {} segundos".format(t2-
t1))
speed.append(t2-t1)
#
===================================================================
==========
# Funcion del usuario + map
#
===================================================================
==========
def op1(x):
return lg10(x)
t1 = time.time()
l2 = list(map(op1, l1))
t2 = time.time()
print("Tiempo usan op1 y map {} segundos".format(t2-t1))
speed.append(t2-t1)
#
===================================================================
==========
# Usar numpy : operacion vectorizada
#
===================================================================
==========
a1 = np.array(l1)
t1 = time.time()
a2 = np.log10(a1)
t2= time.time()
print("Tiempo usando numpy {} segundos".format(t2-t1))
speed.append(t2-t1)

#
===================================================================
==========
# Realizar un diagrama de barras : speed
#
===================================================================
==========
plt.figure(figsize = (10,6))
plt.title("Procesamiento de 1Millon de datos")
plt.xlabel("Algoritmos")
plt.ylabel("Tiempo [s.]")
plt.grid(True)
plt.bar(x = range(1,5),
height = speed,
align = 'center',
tick_label = ["Bucle For", "Comprehension de listas" , "Op1
+ map" ,
"Numpy"])

#%%

You might also like