Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: use size-zero dtype for broadcast-shapes #26599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024

Conversation

mhvk
Copy link
Contributor

@mhvk mhvk commented Jun 2, 2024

In #26160, the performance of broadcast_shapes was improved by replacing a dtype=[] with dtype=bool. The reason this worked is that the conversion is faster (i.e., np.dtype(bool) is faster than np.dtype([]), but this has the side effect that empty arrays real sizes are created - and hence broadcast_shapes is now considerable slower than it was for large-sized arrays. This PR changes it to a definition of the dtype outside of the function, which has the best of both worlds:

# Pre-26160
In [1]: %timeit np.broadcast_shapes((6, 7), (5, 6, 1), (7,), (5, 1, 7))
2.28 µs ± 2.64 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [2]: %timeit np.broadcast_shapes((6, 7000000), (5, 6, 1), (7000000,), (5, 1, 7000000))
2.29 µs ± 3.89 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

# With 26160
In [1]: %timeit np.broadcast_shapes((6, 7), (5, 6, 1), (7,), (5, 1, 7))
2 µs ± 7.77 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [2]: %timeit np.broadcast_shapes((6, 7000000), (5, 6, 1), (7000000,), (5, 1, 7000000))
10.8 µs ± 9.43 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

# With this PR
In [1]: %timeit np.broadcast_shapes((6, 7), (5, 6, 1), (7,), (5, 1, 7))
2.02 µs ± 2.54 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [2]: %timeit np.broadcast_shapes((6, 7000000), (5, 6, 1), (7000000,), (5, 1, 7000000))
2.04 µs ± 3.48 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

This makes the speed independent of the actual shapes
(as it used to be before numpygh-26160), but still fast.
@mhvk mhvk force-pushed the broadcast-size-0-array branch from ce8775e to 7a647ea Compare June 2, 2024 18:15
@mhvk mhvk changed the title MAINT: use size-zero dtype for broadcast-shapes ENH: use size-zero dtype for broadcast-shapes Jun 2, 2024
@seberg seberg merged commit a2d1972 into numpy:main Jun 3, 2024
67 of 68 checks passed
@seberg
Copy link
Member

seberg commented Jun 3, 2024

Thanks for the follow up!

@mhvk mhvk deleted the broadcast-size-0-array branch June 3, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants