[Tutor] Loop comparison

Dave Angel davea at ieee.org
Fri Apr 16 15:37:03 CEST 2010

>> The precalculation optimisations are 
>> taking place.  If you pass it an argument to use for the upper limit of the 
>> sequence the calculation time shoots up.
> I'm still confused about when the addition takes place. 
> Surely the compiler has to do the addition, so it should be slower?
> I assume you have to run the posted code through cython
> prior to running it in Python?
> You can probably tell that I've never used Cython! :-)
> Alan G.
I've never used Cython either, but I'd guess that it's the C compiler 
doing the extreme optimizing.  If all the code, including the loop 
parameters, are local, non-volatile, and known at compile time, the 
compile could do the arithmetic at compile time, and just store a result 
like    res = 42;

Or it could notice that there's no I/O done, so that the program has 
null effect.  And optimize the whole thing into a "sys.exit()"

I don't know if any compiler does that level of optimizing, but it's 
certainly a possibility.  And such optimizations might not be legitimate 
in stock Python (without type declarations and other assumptions), 
because of the possibility of other code changing the type of globals, 
or overriding various special functions.


More information about the Tutor mailing list