Speed-up for loops

Stefan Behnel stefan_ml at behnel.de
Fri Sep 3 05:39:21 EDT 2010


Michael Kreim, 03.09.2010 11:21:
> So finally I followed the recommendation of Tim Wintle to use cython. I
> did not know this before, but I figured out the following:
> additionWintle2.pyx:
 >
> def addition():
>     cdef long imax = 1000000000
>     cdef long a = 0
>     cdef long i
>     for i in xrange(imax):
>        a = a + 10
>     print a
>
> => runs (wall clock time): 0:00.04

Note that this isn't the "real" runtime. If you look up the binary code 
that the C compiler spits out, you'll most likely find the final result for 
"a" written down as a literal that gets returned from the function. C 
compilers do these things to benchmarks these days.

Stefan




More information about the Python-list mailing list