Speed of Python
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Fri Sep 7 19:17:55 EDT 2007
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]
More information about the Python-list
mailing list