Speed of Python
ajaksu
ajaksu at gmail.com
Fri Sep 7 21:27:45 CEST 2007
On Sep 7, 2:37 pm, "wang frank" <f... at hotmail.co.jp> wrote:
> I am not familiar with python, so I just simply try to reproduce the same
> code in python.
Seems almost correct, but from what I guess of MatLab, George's
suggestions make it a bit more fair.
> If you think that my python script is not efficient, could you tell me how
> to make it more efficient?
In pure Python? No idea (besides using Roberto's and George's
suggestions). If you allow for extensions, Istvan has the answer. If
you allow compiling Python to C++ (using ShedSkin: http://shed-skin.blogspot.com/),
here's a small report:
----------
ajaksu at Belkar:~/sandbox$ cat bench.py
import math
n = 1
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
a = bench1(10)
ajaksu at Belkar:~/sandbox$ ss -e bench.py
*** SHED SKIN Python-to-C++ Compiler 0.0.22 ***
Copyright 2005-2007 Mark Dufour; License GNU GPL version 2 (See
LICENSE)
(Please send bug reports here: mark.dufour at gmail.com)
[iterative type analysis..]
**
iterations: 2 templates: 44
[generating c++ code..]
ajaksu at Belkar:~/sandbox$ make bench.so
g++ -O3 -s -pipe -fomit-frame-pointer -I/home/ajaksu/shedskin-0.0.22/
lib -g -fPIC -I/usr/include/python2.5 -D__SS_BIND /home/ajaksu/
shedskin-0.0.22/lib/builtin.cpp /home/ajaksu/shedskin-0.0.22/lib/
math.cpp bench.cpp -lgc -shared -Xlinker -export-dynamic -lpython2.5 -
o bench.so
ajaksu at Belkar:~/sandbox$ mv bench.py pbench.py
ajaksu at Belkar:~/sandbox$ ipython
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[...]
In [1]: from pbench import bench1 as pbench1
In [2]: from bench import bench1
In [3]: %timeit a = bench1(10)
100 loops, best of 3: 10.2 ms per loop
In [4]: %timeit a = pbench1(10)
10 loops, best of 3: 92.8 ms per loop
----------
I guess you'd also see nice improvements from Pyrex or Cython, Blitz
and other tools. Check http://wiki.python.org/moin/PythonSpeed/PerformanceTips
for the general ideas and http://scipy.org/PerformancePython for an
insight on available tools that even compares their speeds to Matlab.
And-if-you-run-more-benchmarks-please-do-post-them-ly yrs,
Daniel
More information about the Python-list
mailing list