même si cette méthode (en une ligne) implique plusieurs parcours de chaines, cela reste la plus rapide. Je suppose que cela est du au fait que split soit écrit et optimisé en C. Ma première méthode quand à elle est loin derrière (même sans redéfinition constante de txt). Si la structure de ton texte obéit donc aux conditions ennoncées par nardo47, alors sa méthode est carrément la plus appropriée.
chaine testée 5000 fois:
txt = ''.join(['%s[xxx]'%hex(x) for x in xrange(100)])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 6.444 6.444 <string>:1(<module>)
1 0.000 0.000 6.443 6.443 crochets.py:1(<module>)
5000 0.863 0.000 0.863 0.000 crochets.py:1(me1)
500000 1.806 0.000 1.806 0.000 crochets.py:12(isThereEnd)
5000 2.534 0.001 5.362 0.001 crochets.py:20(me2)
5000 0.035 0.000 0.180 0.000 crochets.py:35(nardo)
10000 0.042 0.000 0.087 0.000 crochets.py:36(<genexpr>)
1 0.037 0.037 6.443 6.443 crochets.py:39(main)
1 0.001 0.001 6.444 6.444 {execfile}
100 0.000 0.000 0.000 0.000 {hex}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Prof
iler' objects}
505000 1.023 0.000 1.023 0.000 {method 'find' of 'str' objects}
5001 0.044 0.000 0.131 0.000 {method 'join' of 'str' objects}
10000 0.060 0.000 0.060 0.000 {method 'split' of 'str' objects} |
Si ma dernière méthode possède un equivalent regex, alors re resterait sûrement préférable.
Partager