Python 'for' loop is memory inefficient

Carl Banks pavlovevidence at
Mon Aug 17 03:53:10 CEST 2009

On Aug 16, 6:28 pm, exar... at wrote:
> On 01:23 am, benjamin.kap... at wrote:
> >On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden <sturlamol... at>
> >wrote:
> >>A compiler could easily recognise a statement like
> >>  for i in range(n):
> >>as a simple integer loop. In fact, Cython is able to do this.
> >but special cases aren't special enough to break the rules.
> Although I think PyPy also recognizes this case and makes it as
> efficient as using xrange, and does so without breaking any rules.

PyPy uses a JIT compiler (which is still slower than CPython,
suggesting that perhaps they should have spent more time optimizing
the general case than optimizing for an easily avoided special case).

> There *are* _some_ legitimate complaints to be made about the CPython
> runtime. :)

This isn't one of them.

xrange has been part of Python for 10 years.

If there are any complaints to be made about this situation it's that
there are any 2.x learning materials anythere that continue to use
range() and not xrange() in this context.

Carl Banks

More information about the Python-list mailing list