Speed-up for loops

Michael Kreim michael at perfect-kreim.de
Fri Sep 3 11:21:36 CEST 2010


Hi,

thanks a lot for your answers. I learn a lot and I like to sum up your 
suggestions and show you the results of the time command on my machine:

Original code by me:
imax = 1000000000
a = 0
for i in xrange(imax):
     a = a + 10
print a
=> runs (wall clock time): 1:55.14

Peter Otten suggested to put the code into a function:
def f():
     imax = 1000000000
     a = 0
     for i in xrange(imax):
         a = a + 10
     print a
f()
=> runs (wall clock time):  0:47.69

Tim Wintle and Philip Bloom posted some code using a while loop:
imax = 1000000000
a = 0
i = 0
while 1:
     i = i + 1
     if (i > imax):
         break
     a = a + 10
print a
=> runs (wall clock time):  3:28.05

imax = 1000000000
a = 0
i = 0
while(i<imax):
     a = a + 10
     i = i + 1
print a
=> runs (wall clock time):  3:27.74


Hrvoje Niksic suggested the usage of itertools:
from itertools import repeat
imax = 1000000000
a = 0
for i in repeat(None, imax):
     a = a + 10
print a
=> runs (wall clock time): 1:58.25


I wrote a code combining these:
def f():
     from itertools import repeat
     imax = 1000000000
     a = 0
     for i in repeat(None, imax):
         a = a + 10
     print a
f()
=> runs (wall clock time): 0:43.08

Then Roland Koebler suggested psyco but I am sitting on a 64bit machine 
and so I could not test it (although it looks promising).

An anonymous Nobody suggested to use Numpy. I did not do this, because I 
am very very new to Numpy and I did not figure out a Numpy specific way 
to do this. Maybe a Numpy expert has something for me?

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

setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("additionWintle2", ["additionWintle2.pyx"])]
setup(
   name = 'Cython test',
   cmdclass = {'build_ext': build_ext},
   ext_modules = ext_modules
)

$ python setup.py build_ext --inplace

run.py:
from additionWintle2 import addition
addition()
running build_ext

=> runs (wall clock time):  0:00.04


And to compare this. I wrote something similar in Matlab and C++ 
(although some authors, pointed out that it is not that easy to compare 
"for" loops in these three languages):
addition.cpp
#include <iostream>
using namespace std;
int main()
{
     long imax = 1e9;
     long a = 0;
     long i;
     for(i=0; i < imax; i++)
     {
         a = a + 10;
     }
     cout << a << endl;
     return 0;
}
=> Elapsed (wall clock) time (h:mm:ss or m:ss): 0:02.32

addition.m
imax = 1e9;
a = 0;
for i=0:imax-1
     a = a + 10;
end
disp(a);
exit;
=> Elapsed (wall clock) time (h:mm:ss or m:ss): 0:08.39


With best regards,

Michael





More information about the Python-list mailing list