efficiency of range() and xrange() in for loops
mensanator at aol.com
mensanator at aol.com
Wed Apr 5 17:15:11 EDT 2006
Todd wrote:
> 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
Didn't it occur to you to also check memory usage?
I learned about xrange the hard way, when my range ate up all
available memory.
More information about the Python-list
mailing list