[Tutor] Loop comparison

Stefan Behnel stefan_ml at behnel.de
Fri Apr 16 10:35:26 CEST 2010


Stefan Behnel, 16.04.2010 09:38:
> A compiler for a statically compiled language can see that the
> above loop yields a constant result, so it can calculate the result in
> advance (or at least reduce the loop overhead for the calculation)
> instead of generating code for the loop as it stands.

That reminds me of an optimisation anecdote I experienced during a talk I 
gave on Cython. I was showing how a couple of simple type annotations could 
be used to make Cython run arithmetic code a couple of hundred times faster 
than it runs in CPython. A tiny little change at the end brought the factor 
from about 200x to an increadible 18000x. That was still one order of 
magniture larger than the highest speedup I had seen from Cython up to 
then, so I was perplex at the time and had to take a deeper look.

It turned out that this was exactly the point where Cython had enough type 
annotations available to make the Python code turn completely into C code, 
so the C compiler noticed that the whole benchmark code didn't actually 
yield any result, optimised away the whole thing and just wrote out an 
empty module. A tremendous speedup, obviously...

Stefan



More information about the Tutor mailing list