Skip to content

Instantly share code, notes, and snippets.

@TopRoupi
Created November 16, 2019 15:52
Show Gist options
  • Save TopRoupi/f1be861c9a99ec7c3c2b5beeb99d2d67 to your computer and use it in GitHub Desktop.
Save TopRoupi/f1be861c9a99ec7c3c2b5beeb99d2d67 to your computer and use it in GitHub Desktop.
from random import sample
import multiprocessing
from copy import copy, deepcopy
from time import clock, time, sleep
from pylab import plot, title, legend, xlabel, ylabel, grid, show, ticklabel_format, hist, scatter, axis
import sys
def tempo(f,*a):
'''Devolve o tempo de execução (aproximado) de uma função f com argumentos *a'''
i = time()
f(*a)
return time()-i
def executar_teste(t):
print('processando %s com uma lista de %d' %(t[0].__name__, len(t[1])))
return tempo(t[0],t[1])
def cpu_p(*F,N=(500,5000,500)):
'''Mostra gráfico com o consumo de tempo das funções *F, para
entradas com tamanho variando no intervalo N=(500,5000,500).
Um parâmetro E = g define a função que gera as entradas'''
C = ['r','b','g','c','m','y','k','w']
M = ['o','s','p','h','+','x','v','^']
X = []
Y = [[] for _ in range(len(F))]
inicio = time()
for n in range(N[0],N[1]+1,N[2]):
X.append(n)
for i,f in enumerate(F):
Y[i].append(
(f,
sample(range(0,n),n))
)
for i in range(len(Y)):
pool = multiprocessing.Pool()
Y[i] = pool.map(executar_teste, Y[i])
print('\nTempo total: %.1fs' % (time()-inicio))
xlabel('tamanho da entrada')
ylabel('tempo de execução (s)')
ticklabel_format(style='sci', axis='both', scilimits=(-3,+4))
grid(True)
for i,f in enumerate(F):
plot(X,Y[i],color=C[i%len(C)],marker=M[i%len(M)],label=f.__name__)
legend(loc='upper left')
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment