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

Keith Goodman kwgoodman@gmail....
Fri Sep 5 11:22:15 CDT 2008


On Fri, Sep 5, 2008 at 9:08 AM, David Cournapeau <cournape@gmail.com> wrote:
> 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)

I was comparing the two techniques from this question:

Ludwig wrote:
> What are the relative merits of
>
> sum(a[where(a>0])
>
> to
>
> a[a > 0].sum()


More information about the Numpy-discussion mailing list