On Wed, 2021-04-14 at 18:53 -0700, dan_patterson wrote:
> a = np.zeros(1_000_000)
> 
> a[100] = 1
> 
> %timeit np.any(a)
> 814 µs ± 17.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops
> each)
> 
> %timeit np.any(a == 1)
> 488 µs ± 5.68 µs per loop (mean ± std. dev. of 7 runs, 1000 loops
> each)
> 

It may be worth checking which ufunc loop is used (and what casts are
necessary due to that), I am not sure immediately.
Casting (and the fact that it cannot be "short circuited right now),
should be at the core of understanding the speeds properly.

We might be able to short-circuit more in the future, or add new loops
to avoid casting.  In the end it is annoying that `np.any(a)` can be
slower than `np.any(a != 0)`, but it is an additional
complexity/project to think about optimizing `np.any(a == 1)` if you
want short circuiting, you cannot do the `a == 1` greedily after all.

Cheers,

Sebastian


> Haven't investigated further since your times are much longer than
> mine and
> secondly the equality check for 1 implies that perhaps a short
> circuit
> actually exists somewhere 
> 
> 
> 
> --
> Sent from: http://numpy-discussion.10968.n7.nabble.com/
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion

Reply via email to