Why not a Python compiler?

Stefan Behnel stefan_ml at behnel.de
Sun Feb 10 08:29:47 EST 2008


Steven D'Aprano wrote:
> On Sat, 09 Feb 2008 01:11:09 +0000, Marc 'BlackJack' Rintsch wrote:
> 
>> On Fri, 08 Feb 2008 05:12:29 -0800, Ryszard Szopa wrote:
>>
>>> Expressing simple loops as C for loops...
>> You mean simple loops like ``for i in xrange(1000):``?  How should the
>> compiler know what object is bound to the name `xrange` when that loop
>> is executed?
> 
> Assuming the aim is to optimize for speed rather than memory, the 
> solution is for the compiler to create something like this pseudo-code:
> 
> if xrange is Python's built-in xrange:
>     execute optimized for-loop at C-like speed
> else:
>     execute unoptimized normal loop at Python speed
> 
> (In case it's not obvious, the decision of which branch to take is made 
> at run time, not compile time.)
> 
> I understand that is more or less what psycho already does.

... and Cython, when iterating over lists, for example. That's one of the
reasons why looping is so much faster in Cython than in Pyrex.

Stefan



More information about the Python-list mailing list