efficiency of range() and xrange() in for loops

Todd goldfita at signalsguru.net
Wed Apr 5 16:54:16 EDT 2006


Steve R. Hastings wrote:
> When you compile the expression
>
>     for i in range(1000):
>         pass
>
> does Python make an iterator for range(), and then generate the values
> on the fly?  Or does Python actually allocate the list [0, 1, 2, ..., 999]
> and then step through it?

I ran an experiment on this a while back.  I thought it generated a
list.  But the results turned out to be surprising.  Take this with a
grain of salt.  I'm not completely sure I did what I meant to do.  But
it looks like however you do it, it works out about the same.

http://www.signalsguru.net/articles/pyloops/pyloops.html




More information about the Python-list mailing list