I also have tried to use numpy to speed it up. However, surprisingly, it is slower than the pure python code.

Here is the code:
import numpy arange=numpy.arange
nlog=numpy.log
def bench6(n):
        for i in xrange(n):
                for j in xrange(1000):
                        m=j+1
                        z=nlog(m)
                        z1=nlog(m+1)
                        z2=nlog(m+2)
                        z3=nlog(m+3)
                        z4=nlog(m+4)
                        z5=nlog(m+5)
                        z6=nlog(m+6)
                        z7=nlog(m+7)
                        z8=nlog(m+8)
                        z9=nlog(m+9)
        return z9       

from math import log
def bench3(n):
        for i in xrange(n):
                for j in xrange(1000):
#                       m=j+1
                        z=log(j+1)
                        z1=log(j+2)
                        z2=log(j+3)
                        z3=log(j+4)
                        z4=log(j+5)
                        z5=log(j+6)
                        z6=log(j+7)
                        z7=log(j+8)
                        z8=log(j+9)
                        z9=log(j+10)
        return z9               

Here is the result:

t6=timeit.Timer("bench1.bench6(10)", "import bench1")
t6.repeat(1,1)
[0.73878858905254674]
t3=timeit.Timer("bench1.bench3(10)", "import bench1")
t3.repeat(1,1)
[0.056632337350038142]


Anyone know why?

Thanks

Frank


From: "Kurt Smith" <[EMAIL PROTECTED]>
To: "wang frank" <[EMAIL PROTECTED]>
Subject: Re: Speed of Python
Date: Fri, 7 Sep 2007 16:49:05 -0500

On 9/7/07, wang frank <[EMAIL PROTECTED]> wrote:
> Hi,
> Here is the matlab code:
> function [z]=bench1(n)
> for i=1:n,
>     for j=1:1000,
>         z=log(j);
>         z1=log(j+1);
>         z2=log(j+2);
>         z3=log(j+3);
>         z4=log(j+4);
>         z5=log(j+5);
>         z6=log(j+6);
>         z7=log(j+7);
>         z8=log(j+8);
>         z9=log(j+9);
>     end
> end
> z = z9;
>
> I am not familiar with python, so I just simply try to reproduce the
same
> code in python.
> If you think that my python script is not efficient, could you tell me
how
> to make it more efficient?

One thing you can do is bind math.log to the function's namespace thusly:

import math
def bench1_opt(n):
    log = math.log
    for i in range(n):
        for j in range(1000):
            m=j+1
            z=log(m)
            z1=log(m+1)
            z2=log(m+2)
            z3=log(m+3)
            z4=log(m+4)
            z5=log(m+5)
            z6=log(m+6)
            z7=log(m+7)
            z8=log(m+8)
            z9=log(m+9)
    return z9

On my system I get about a 20% speedup over the 'unoptimized' version
(even though this optimization is rather trivial and may even help
readability).  Still not matlab speed, but better.  You might be able
to do better using xrange instead of range, but the loop overhead
isn't the main slowdown (only about 1% ).

For comparisons in real-world usage (if you are doing numerical work),
I urge you to take a look at a specifically numerical package --
numpy/scipy or their equivalents: http://www.scipy.org/  Python is a
*very* general language not suited for heavy numerical work out of the
box -- dedicated numerical packages adapt python to this specialized
envirornment, and are becoming more and more competitive with Matlab.
The best part is you can put your time-critical code in FORTRAN or C
and wrap it with pyrex, f2py, weave, etc. pretty easily, and still
have the beauty of Python gluing everything together.

Kurt

_________________________________________________________________
メッセンジャー用アイコンに大人気オンラインゲームの萌えなキャラが登場! http://messenger.live.jp/
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to