On Fri, 07 Sep 2007 22:59:26 +0000, wang frank wrote:

> 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       
> 
> […]
> 
> Anyone know why?

Because you don't really take advantage of `numpy`.  The `numpy.log()`
function can be used with scalars but I guess it is slower because it has
to check if its argument is a scalar or array.  Untested:

from numpy import arange, log as nlog

def bench6(n):
    for dummy in xrange(n):
        for j in xrange(1000):
            z = nlog(arange(j + 1, j + 11))
    return z[-1]
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to