I found this on the net, and it's a different arrangement to using for

the author claims it will result in faster performance, but i can't find
documents on it, because I can't figure out which parts are parameters and
which parts on special words, for python.
The operations with explicit loop counters have to be translated into Python 
byte code and must be interpreted. Implicit loop counter can be incremented by 
the core code (translated from C sources). Try this:

import timeit

sWhile = """\
x = 0
y = 0
number_scanned = 0
while x < 1024:
    while y < 768:
        y = y + 1
        number_scanned = number_scanned + 1
    y = 0
    x = x + 1
"""
t = timeit.Timer(stmt=sWhile)
print "%.2f usec/pass" % (1000000 * t.timeit(number=10)/10)


sFor = """\
number_scanned = 0
for x in xrange(1024):
    for y in xrange(768):
        number_scanned = number_scanned + 1
"""
t = timeit.Timer(stmt=sFor)
print "%.2f usec/pass" % (1000000 * t.timeit(number=10)/10)


The sWhile and sFor are the multiline strings containing the equivalent code 
written using while and for loop respectively.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to