[Numpy-discussion] fast way of doing "cross-multiplications" ?
Sasha
ndarray at mac.com
Tue Jul 18 12:34:24 EDT 2006
On 7/18/06, Eric Emsellem <emsellem at obs.univ-lyon1.fr> wrote:
[...]
> (is "sum" different than "add.reduce"?)
"sum" is a wrapper around ndarray.sum method, while add.reduce is a
ufunc method. At the C level they are the same, but "sum" may be a
little slower for the small arrays.
> python -m timeit -s "from numpy import add, zeros, sum; x = zeros(10)" "sum(x)"
100000 loops, best of 3: 5.53 usec per loop
> python -m timeit -s "from numpy import add, zeros, sum; x = zeros(10)" "add.reduce(x)"
100000 loops, best of 3: 2.71 usec per loop
In the new code, I would recommend using the ndarray.sum method
instead of either of the two.
BTW, is this an optimization opportunity: compare
> python -m timeit -s "from numpy import add, zeros, sum; x = zeros(10); r=add.reduce" "r(x)"
100000 loops, best of 3: 2.55 usec per loop
and
> python -m timeit -s "from numpy import zeros; x = zeros(10)" "x.sum()"
100000 loops, best of 3: 3.88 usec per loop
?
More information about the NumPy-Discussion
mailing list