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
| #!/usr/bin/env python3
# coding: utf-8
import random
import timeit
from functools import partial
def tpl(t):
return tuple(max(x) for x in zip(*t))
def lst(t):
res=[]
for x in zip(*t):
res.append(max(x))
return res
def get_max(t):
for elem in zip(*t):
yield max(*elem)
def with_other_fct(t):
return [x for x in get_max(t)]
# Le tableau témoin
temoin=[
[1, 2, 3, 8, 4],
[3, 4, 5, 2, 3],
[9, 1, 4, 7, 9],
]*10
# Les fonctions à chronométrer
fct={
"comprehension" : tpl,
"append" : lst,
"with_other_fct" : with_other_fct,
}
# Le benchmark
for (k, v) in random.sample(fct.items(), len(fct)):
t=timeit.Timer(partial(v, temoin)).repeat(repeat=10)
print("%s: min=%f, max=%f, avg=%f" % (k, min(t), max(t), sum(t)/len(t)))
# for |
Partager