[Numpy-discussion] sum of positive values in an array

David Cournapeau cournape@gmail....
Fri Sep 5 11:08:54 CDT 2008


On Fri, Sep 5, 2008 at 11:52 PM, Keith Goodman <kwgoodman@gmail.com> wrote:

> Here's another difference:
>
>>> a = np.random.randn(100000)
>>> timeit np.sum(a[np.where(a>0)])
> 100 loops, best of 3: 3.44 ms per loop
>>> timeit a[a > 0].sum()
> 100 loops, best of 3: 2.21 ms per loop

But you're not comparing the same thing: why calling where in one case
and not in the other ? The difference is in the where call, not in the
a.sum() vs sum(a)

cheers,

David


More information about the Numpy-discussion mailing list