[Numpy-discussion] Is sum() slow?

Mads Ipsen mpi at osc.kiku.dk
Mon Mar 6 23:16:02 EST 2006


Hi,

Sorry if this is a double post - unsure if it made it the first time:

Here are some timings that puzzle me a bit. Sum the two rows of a 2xn
matrix, where n is some large number

python -m timeit -s "from numpy import array,sum,reshape; x =
array([1.5]*1000); x = reshape(x,(2,500))" "x.sum(0)"
10000 loops, best of 3: 36.2 usec per loop

python -m timeit -s "from numpy import array,sum,reshape; x =
array([1.5]*1000); x = reshape(x,(2,500))" "x[0] + x[1]"
100000 loops, best of 3: 5.35 usec per loop

The two calculations are completely equivalent (at least from the
numerical results point of view). But using the built-in sum() function is
app. 6 times slower.

Just for the reference - using Numeric gives

python -m timeit -s "from Numeric import array,sum,reshape; x =
array([1.5]*1000); x = reshape(x,(2,500))" "sum(x)"
100000 loops, best of 3: 7.08 usec per loop

Any suggestions to why this might be - or am I doing something wrong here?

Thanks // Mads

PS. Thanks to everybody that responded to my previous posting on problems
and questions regarding rint() and related coding issues. Your replies
were very helpful!





More information about the NumPy-Discussion mailing list