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




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

.



Relevant Pages

  • Re: simultaneous assignment
    ... Steve R. Hastings wrote: ... False and 0 do not directly compare ... will work in Python 2.2 and newer. ...
    (comp.lang.python)
  • Re: maximum() efficency
    ... Steve R. Hastings wrote: ... The built-in max ... Are you sure max can't solve it with an appropriate decorate-sort-undecorate or the key= argument coming in Python 2.5? ...
    (comp.lang.python)
  • Re: any() and all() on empty list?
    ... Steve R. Hastings wrote: ... Python, and then we can just use it and not worry about how to write it. ...
    (comp.lang.python)
  • Re: efficiency of range() and xrange() in for loops
    ... Steve R. Hastings wrote: ... does Python make an iterator for range, ... If Python actually allocates the list, then clearly we should all use ... A patch for the old compiler existed on SF, ...
    (comp.lang.python)
  • Re: Python needs a CPyAN
    ... Steve> without a CPyAN Python will never even get close to ... Steve> achieving the degree of widespread usage that Perl ... Steve> enable Python to flourish in the coming decade. ...
    (comp.lang.python)