Speed-up for loops
michael at perfect-kreim.de
Thu Sep 2 12:02:40 CEST 2010
I was comparing the speed of a simple loop program between Matlab and
$ cat addition.py
imax = 1000000000
a = 0
for i in xrange(imax):
a = a + 10
$ cat addition.m
imax = 1e9;
a = 0;
a = a + 10;
The results look like this:
$ /usr/bin/time --verbose python addition.py
Command being timed: "python addition.py"
User time (seconds): 107.30
System time (seconds): 0.08
Percent of CPU this job got: 97%
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:50.09
$ /usr/bin/time --verbose matlab -nodesktop -nosplash -r "addition"
Command being timed: "matlab -nodesktop -nosplash -r addition"
User time (seconds): 7.65
System time (seconds): 0.18
Percent of CPU this job got: 94%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:08.25
Unfortunately my Python Code was much slower and I do not understand why.
Are there any ways to speed up the for/xrange loop?
Or do I have to live with the fact that Matlab beats Python in this example?
Thanks a lot for your answers.
With best regards,
More information about the Python-list