Why not a Python compiler?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Feb 9 02:49:10 CET 2008


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.)

This, naturally, assumes that the test of whether xrange is the built-in 
xrange is fast. If it is not, then the optimized code will actually be 
slower for small enough loops. But that's hardly unusual: no optimizing 
compiler guarantees to optimize code in every possible case.

I understand that is more or less what psycho already does.


-- 
Steven



More information about the Python-list mailing list