[Numpy-discussion] Improving performance of the `numpy.any` function.
sebastian at sipsolutions.net
Thu Apr 15 12:42:18 EDT 2021
On Wed, 2021-04-14 at 18:53 -0700, dan_patterson wrote:
> a = np.zeros(1_000_000)
> a = 1
> %timeit np.any(a)
> 814 µs ± 17.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops
> %timeit np.any(a == 1)
> 488 µs ± 5.68 µs per loop (mean ± std. dev. of 7 runs, 1000 loops
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.
> Haven't investigated further since your times are much longer than
> mine and
> secondly the equality check for 1 implies that perhaps a short
> actually exists somewhere
> Sent from: http://numpy-discussion.10968.n7.nabble.com/
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 833 bytes
Desc: This is a digitally signed message part
More information about the NumPy-Discussion