Speed of Python

wang frank fw3 at hotmail.co.jp
Fri Sep 7 16:25:41 EDT 2007


Thanks for all your help.

Using Istvan Albert's suggestion, I have recompared the speed of the 
following funciton:
matlab:
function [z]=bench2(n)
for i=1:n,
    j=(0:999);
    z=log(j+1);
end 

python:
from  numpy import arange, log
def bench4(n):
    for i in xrange(n):
        nums = arange( n )
        a = log( nums + 1)

Python actually is faster than Matlab.

tic; z=bench2(1000); toc
Elapsed time is 0.159485 seconds.

Python:
>>> import timeit
>>> t=timeit.Timer("bench1.bench4(1000)","import bench1")
>>> t.repeat(1,1)
[0.10052953657517924]

Thanks again.

Frank
>From: ajaksu <ajaksu at gmail.com>
>To: python-list at python.org
>Subject: Re: Speed of Python
>Date: Fri, 07 Sep 2007 19:27:45 -0000
>
>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
>


>--
>http://mail.python.org/mailman/listinfo/python-list

_________________________________________________________________
3つの”ホンモノ”プレゼント 賞品第3弾スタート!アルファ スパイダー2.2が当た
る http://clk.atdmt.com/GBL/go/msnjpqjl0060000010gbl/direct/01/ 




More information about the Python-list mailing list