On Thu, Sep 2, 2010 at 7:02 PM, Michael Kreim <mich...@perfect-kreim.de> wrote: > Hi, > > I was comparing the speed of a simple loop program between Matlab and > Python. > > My Codes: > $ cat addition.py > imax = 1000000000 > a = 0 > for i in xrange(imax): > a = a + 10 > print a > > $ cat addition.m > imax = 1e9; > a = 0; > for i=0:imax-1 > a = a + 10; > end > disp(a); > exit; > > The results look like this: > $ /usr/bin/time --verbose python addition.py > 10000000000 > 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" > [...] > 1.0000e+10 > 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.
Getting the above kind of code fast requires the interpreter to be clever enough so that it will use native machine operations on a int type instead of converting back and forth between internal representations. Matlab since version 6 I believe, has a JIT to do just that. There is no mature JIT-like implementation of python which will give you the same speed up for this exact case today. > Or do I have to live with the fact that Matlab beats Python in this example? Yes. Without a JIT, python cannot hope to get the same kind of speeds for this kind of examples. That being said, neither matlab nor matlab are especially good at doing what you do in your example - for this exact operation, doing it in C or other compiled languages will be at least one order of magnitude faster. Generally, you use matlab's vectorized operations, and in that case, numpy gives you similar performances (sometimes faster, sometimes slower, but in the same ballpark in general). cheers, David -- http://mail.python.org/mailman/listinfo/python-list