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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| from timeit import Timer
repet = 5
iterat = 1000
def affich(ch,ts):
print ch.rjust(30)+' : ','min',str(min(ts))[0:7],' moy',str(sum(ts)/repet)[0:7],' max',str(max(ts))[0:7]
ch = 'jhgufwkjdhfferutbjshvipodqferitlkhsd'
def fait_set(x):
return set(x)
print fait_set(ch)
tfait_set = Timer('fait_set(ch)','from __main__ import fait_set,ch').repeat(repet,iterat)
affich('fait_set',tfait_set)
def fait_add(x):
e = set([])
for x in ch:
e.add(x)
return e
print fait_add(ch)
tfait_add = Timer('fait_add(ch)','from __main__ import fait_add,ch').repeat(repet,iterat)
affich('fait_add',tfait_add)
def update_set(x):
f = set([])
f.update(ch)
return f
print update_set(ch)
tupdate_set = Timer('update_set(ch)','from __main__ import update_set,ch').repeat(repet,iterat)
affich('update_set',tupdate_set)
print '---------------------------------------------------------'
A = set('abcdefghijklmnopq')
B = set('bxgrlwojqzdytu')
def intersecte(A,B):
I = set([])
for x in A:
if x in B:
I.add(x)
return I
print intersecte(A,B)
tintersecte = Timer('intersecte(A,B)','from __main__ import intersecte,A,B').repeat(repet,iterat)
affich('intersecte',tintersecte)
def intersect_update(A,B):
A.intersection_update(B)
return A
print intersect_update(A,B)
tintersect_update = Timer('intersect_update(A,B)','from __main__ import intersect_update,A,B').repeat(repet,iterat)
affich('intersect_update',tintersect_update)
print '-------------------------------------------------------------'
L = [(1, 1, 4), (3, 1, 11), (2, 1, 13), (6, 1, 17), (5, 1, 17),(4, 1, 18),(2, 2, 28),
(1, 2, 29), (3, 2, 30), (4, 2, 31),(5, 2, 31), (6, 2, 33),(1, 3, 45), (3, 3, 51),
(2,4,98), (1,2,45), (6,23,98), (6,36,23), (6,34,12), (3,9,21), (6,4,100),
(5,7,8), (2,9,4), (5,3,8), (1,4,9), (1,7,3), (4,7,345), (5,6,76), (6,88,45),
(6,17,23), (1,4,23), (6,5,122), (3,4,7), (2,4,66), (5,34,22), (6,45,3), (0,65,9),
(3,5,8), (2,12,1), (5,50,5), (0,45,2), (3,21,9), (4,5,85), (4,87,3), (4,7,0),
(4,12,8), (2,9,21), (6,9,12), (2,6,34), (5,95,3), (1,6,4), (1,7,45), (2,25,44),
(4,68,3), (2,13,57), (2,64,0), (4,85,9), (3,4,11), (1,45,2), (2,73,8), (5,32,9),
(6,6,4), (6,7,3), (4,7,67), (3,123,9), (3,5,6), (6,32,10), (2,8,67), (6,47,2),
(5,95,23), (1,43,7), (6,94,23), (0,6,45), (5,7,5), (3,9,6), (4,65,3), (4,23,12)]
def dico_operator(L):
d = {}
for x in L:
d[x[0]] = x
return d
print dico_operator(L)
tdico_operator = Timer('dico_operator(L)','from __main__ import dico_operator,L').repeat(repet,iterat)
affich('dico_operator',tdico_operator)
def dico_update_sequence(L):
LL = [ (x[0],x) for x in L ]
dic = {}
dic.update(LL)
return dic
print dico_update_sequence(L)
tdico_update_sequence = Timer('dico_update_sequence(L)','from __main__ import dico_update_sequence,L').repeat(repet,iterat)
affich('dico_update_sequence',tdico_update_sequence)
def dico_update_elements(L):
dic = {}
[dic.update([(x[0],x)]) for x in L]
return dic
print dico_update_elements(L)
tdico_update_elements = Timer('dico_update_elements(L)','from __main__ import dico_update_elements,L').repeat(repet,iterat)
affich('dico_update_elements',tdico_update_elements) |
Partager