Speed-up for loops

Stefan Behnel stefan_ml at behnel.de
Fri Sep 3 22:31:40 CEST 2010


BartC, 03.09.2010 22:17:
> for i in range(N):
>
> which (if I understood correctly) actually created a list of N objects,
> populated it with the values 0, 1, 2...N-1 (presumably using a more
> sensible loop), then iterated between the values of the list!

I guess what applies here is "special cases aren't special enough to break 
the rules". The performance is good enough in most cases, it only hurts 
when the range is large and the loop body is small in comparison, such as 
in the most obvious stupid benchmarks.

Also, xrange() is a pretty old addition the the language and now replaces 
range() in Python 3.

Stefan




More information about the Python-list mailing list