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
| #!/usr/bin/env python3
# coding: utf-8
import random
import timeit
from functools import partial
# Initialisation random
random.seed()
# Les fonctions à tester
def narvalo(l): return list({mot:None for mot in l.split("_")})
def hominide(l):
ll=list()
for mot in l.split('_'):
if mot not in ll: ll.append(mot)
return ll
# hominide()
fct={
"narvalo" : narvalo,
"hominide" : hominide,
}
# Les données à traiter
data = "VOICI_UNE_CHAINE_DE_CARACTERE_COMME_EXEMPLE" * 1000
# Vérification
assert narvalo(data) == hominide(data)
print("Assertion ok")
# Le nombre de répétitions (les moyennes se feront sur cette valeur)
repeat=20
# Appel des fonctions dans un ordre aléatoire et affichage du chrono
print("taille data=%d, repeat=%d" % (len(data), repeat))
for (k, v) in random.sample(tuple(fct.items()), len(fct)):
t=timeit.Timer(partial(v, data)).repeat(repeat=repeat, number=50_000)
print("%s: min=%f, max=%f, avg=%f" % (k, min(t), max(t), sum(t)/len(t)))
# for |
Partager