[Numpy-discussion] Improving performance of the `numpy.any` function.
Sebastian Berg
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[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 at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <https://mail.python.org/pipermail/numpy-discussion/attachments/20210415/3e3d56ea/attachment.sig>
More information about the NumPy-Discussion
mailing list