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 100 101 102 103 104 105 106 107 108 109
| from time import clock
ch0 = 'ju\nju <td align="right"> diderot \n voltaire \nfreron </td> rousseau'\
+' \n russie \n siberie \n silesie\n'\
+'pomme <td> orange \n dentelle\n mimosa\n organdi \n vetyver</td> ocean '\
+'cabane en bois \n cheminee\n sagaie'
import re
pat = re.compile('(<td.*?>.*?</td>)',re.DOTALL)
a=b=c=d=e=100000
for k in xrange(30):
if k%10==0: print k,' ',
# Méthode avec (pos,cnt) et find()
te = clock()
for i in xrange(1000):
liste_avec_find = []
pos = 0
while 1:
x = ch[pos:].find('<td')
y = ch[pos:].find('</td>')
if x+1:
liste_avec_find.append((x+pos,ch0[x:y].count('\n')))
pos += y + 5 # 5 = len('</td>'")
else:
break
ch = ch0
allonge = 0
for (pos,cnt) in liste_avec_find[-1::-1]:
ch = ch[0:pos] + ch[pos:].replace('\n','</br>',cnt)
tf = clock()
a = min(a,tf-te)
boula = (ch==ch1)
ch1 = ch
# Méthode simple avec split()
te = clock()
for i in xrange(1000):
L = re.compile('(<td.*?>.*?</td>)',re.DOTALL).split(ch)
for i in xrange(1,len(L),2):
L[i] = L[i].replace('\n','</br>')
ch = ''.join(L)
tf = clock()
b = min(b,tf-te)
boulb = (ch==ch1)
# # Méthode avec (pos,cnt) rétrograde
te = clock()
for i in xrange(1000):
li = [ (m.start(),m.group().count('\n')) for m in pat.finditer(ch0) ]
ch = ch0
for (pos,cnt) in li[-1::-1]:
ch = ch[0:pos] + ch[pos:].replace('\n','</br>',cnt)
tf = clock()
c = min(c,tf-te)
boulc = (ch==ch1)
# # Méthode avec (d,f)
te = clock()
for i in xrange(1000):
lo = [ m.span() for m in pat.finditer(ch0) ]
ch = ch0
allonge = 0
for (d,f) in lo:
ch = ch[0:d+allonge] + ch0[d:f].replace('\n','</br>') + ch0[f:]
allonge += ch0[d:f].count('\n')*(len('</br>')-len('\n'))
tf = clock()
d = min(d,tf-te)
bould = (ch==ch1)
# Méthode avec (pos,cnt)
te = clock()
for i in xrange(1000):
li = [ (m.start(),m.group().count('\n')) for m in pat.finditer(ch0) ]
ch = ch0
allonge = 0
for (pos,cnt) in li:
ch = ch[0:pos+allonge] + ch0[pos:].replace('\n','</br>',cnt)
allonge += cnt*(len('</br>')-len('\n'))
tf = clock()
e = min(e,tf-te)
boule = (ch==ch1)
# Méthode avec split() , enumerate() et map()
te = clock()
for i in xrange(1000):
ch = ''.join(map(lambda (i,u): [u,u.replace('\n','</br>')][i%2],enumerate(pat.split(ch0))))
tf = clock()
f = min(f,tf-te)
boulf = (ch==ch1)
print
print '\nMéthode avec (pos,cnt) et find() :\n',g(a)
print '\nMéthode simple avec split() :\n',g(b),'=',str((b)*100/a)[0:6],'% de ',a,' ch==ch1 est',boulb
print '\nMéthode avec (pos,cnt) rétrograde :\n',g(c),'=',str((c)*100/a)[0:6],'% de ',a,' ch==ch1 est',boulc
print '\nMéthode avec (d,f) :\n',g(d),'=',str((d)*100/a)[0:6],'% de ',a,' ch==ch1 est',bould
print '\nMéthode avec (pos,cnt) :\n',g(e),'=',str((e)*100/a)[0:6],'% de ',a,' ch==ch1 est',boule
print '\nMéthode avec split() , enumerate() et map() :\n',g(f),'=',str((f)*100/a)[0:6],'% de ',a,' ch==ch1 est',boulf |
Partager