Speed-up for loops

Stefan Behnel stefan_ml at behnel.de
Thu Sep 2 15:25:10 CEST 2010

Tim Wintle, 02.09.2010 14:55:
> If you really need to optimise it then you can convert that module to
> cython by adding a cdef, and then compile it:
> cdef int i
> for i in xrange(imax):
>       a = a + 10
> print a
> or you can write it in C it'll run a lot faster.

Just to get the context right here: a C implementation won't run even a tad 
faster than the obvious Cython version, but both will run "a lot faster" 
than the Python version.

Plus, if Cython knows that the imax value is small enough, it'll infer 
"int" for the "i" variable automatically, so you won't need the "cdef" 
annotation. It won't automatically do that for "a", though, as that might 
break Python's unlimited integer semantics if "imax" and/or "a" are large 


More information about the Python-list mailing list