446,383 Members | 2,063 Online Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

# Speed of Python

 P: n/a Hi, While comparing the speed of octave and matlab, I decided to do a similar test for python and matlab. The result shows that python is slower than matlab by a factor of 5. It is not bad since octave is about 30 time slower than matlab. Here is the result in matlab: Elapsed time is 0.015389 seconds. and in Python: >>t=timeit.Timer("bench1.bench1(10)","import bench1")t.repeat(1,1) [0.071012377266015392] Here is the bench1.py: import math def bench1(n): for i in range(n): for j in range(1000): m=j+1 z=math.log(m) z1=math.log(m+1) z2=math.log(m+2) z3=math.log(m+3) z4=math.log(m+4) z5=math.log(m+5) z6=math.log(m+6) z7=math.log(m+7) z8=math.log(m+8) z9=math.log(m+9) return z9 Is my conclusion correct that Python is slower than matlab? Are there any way to speed it up? It seems Python automatically created bench1.pyc. Does Python automatically execute the bench1.pyc to speed it up? Thanks Frank __________________________________________________ _______________ \$B!VCO?^%^%,!WFC=8(B \$B;D=k\$r?a\$-\$H\$P\$;!*\$4EvCO%"%\$%9%/%j!<%`%^%C%W\$,EP>l(B http://chizumaga.jp/ Sep 7 '07 #1
4 Replies

 P: n/a On Sep 7, 6:42 pm, "wang frank" >t=timeit.Timer("bench1.bench1(10)","impor t bench1") >t.repeat(1,1) [0.071012377266015392] Here is the bench1.py: import math def bench1(n): for i in range(n): for j in range(1000): m=j+1 z=math.log(m) z1=math.log(m+1) z2=math.log(m+2) z3=math.log(m+3) z4=math.log(m+4) z5=math.log(m+5) z6=math.log(m+6) z7=math.log(m+7) z8=math.log(m+8) z9=math.log(m+9) return z9 Is my conclusion correct that Python is slower than matlab? Are there any way to speed it up? It seems Python automatically created bench1.pyc. Does Python automatically execute the bench1.pyc to speed it up? Thanks Frank __________________________________________________ _______________ \$B!VCO?^%^%,!WFC=8(B \$B;D=k\$r?a\$-\$H\$P\$;!*\$4EvCO%"%\$%9%/%j!<%`%^%C%W\$,EP>l(Bhttp://chizumaga.jp/ Sep 7 '07 #2

 P: n/a On Sep 7, 12:42 pm, "wang frank"

 P: n/a On Sep 7, 12:42 pm, "wang frank"

 P: n/a On Sep 7, 12:42 pm, "wang frank" >t=timeit.Timer("bench1.bench1(10)","impor t bench1") >t.repeat(1,1) [0.071012377266015392] Here is the bench1.py: import math def bench1(n): for i in range(n): for j in range(1000): m=j+1 z=math.log(m) z1=math.log(m+1) z2=math.log(m+2) z3=math.log(m+3) z4=math.log(m+4) z5=math.log(m+5) z6=math.log(m+6) z7=math.log(m+7) z8=math.log(m+8) z9=math.log(m+9) return z9 Is my conclusion correct that Python is slower than matlab? Whoa, there, chief, that's a pretty lofty conclusion to make based on one benchmark. A blanket speed comparison between Matlab and Python isn't productive: they each have their own strengths speedwise. (Conversely, a blanket comparison is productive programmingwise. Matlab has almost no strengths relative to Python in that department. But that's another question entirely. :) Roughly speaking, I'd say your average single-pass calculation script is going to be faster in Matlab than in Python. However, Python (with help from numpy) has more opportunities for optimization, especially if you're using large matrices. And if you need to take it up a notch, Python has very good ways to integrate numerical C and Fortran code. Are there any way to speed it up? It seems Python automatically created bench1.pyc. Does Python automatically execute the bench1.pyc to speed it up? Yes, as others have explained. Carl Banks Sep 8 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 