Da gibt's je was zu messen:
Code: Alles auswählen
import timeit
import string
t = list(string.ascii_letters)
def test1():
for no, x in enumerate(t):
foo = no, x
def test2():
for no in xrange(len(t)-1, -1, -1):
foo = no, t[no]
def test3():
for (no, x) in sorted(enumerate(t), reverse=True):
foo = no, x
def test4():
for (no, x) in reversed(list(enumerate(t))):
foo = no, x
for name, func in locals().copy().iteritems():
if not name.startswith("test"):
continue
print name
test = timeit.Timer(func)
print "%.2f" % test.timeit(number=100000)
print
Ausgabe:
test1
0.50
test2
0.53
test3
1.34
test4
0.98
(test1 ist nur zum Vergleich drin)
Die Ergebnisse hätte ich jetzt so nicht ganz erwartet...
Die sorted Variante sieht für mich noch am saubersten aus...