how best to iterate i times ?
Peter Otten
__peter__ at web.de
Wed Mar 17 15:15:31 EST 2004
Randall Smith wrote:
> So, question is: What is the efficient, elegant, pythonic way to
> iterate with integers?
The standard way is to use xrange() instead of range(). If you are
interested only in the repetition, not the numbers, you can also use
itertools.repeat(). Let's see what is fastest:
(1)
for i in xrange(N):
pass
(2)
for _ in repeat(None, N):
pass
$ timeit.py "for i in xrange(100000): pass"
100 loops, best of 3: 1.09e+04 usec per loop
$ timeit.py -s"from itertools import repeat" "for i in repeat(None, 100000):
pass"
100 loops, best of 3: 5.73e+03 usec per loop
Peter
More information about the Python-list
mailing list