Python 'for' loop is memory inefficient

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Mon Aug 17 07:43:15 EDT 2009


On 01:53 am, pavlovevidence at gmail.com wrote:
>On Aug 16, 6:28 pm, exar... at twistedmatrix.com wrote:
>>On 01:23 am, benjamin.kap... at case.edu wrote:
>>
>> >On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden <sturlamol... at yahoo.no>
>> >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).

It's true that PyPy has a JIT compiler (which is still slower than 
CPython).  However, this isn't where the range speedup comes from.  The 
range optimization is handled specifically in the implementation of 
range (or possibly of list, or a combination of the two, I can't 
remember exactly).  It's effective even when the JIT compiler is 
disabled.
>
>>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.

Jean-Paul



More information about the Python-list mailing list