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
ENH: Improve performance of np.broadcast_arrays and np.broadcast_shapes #26160
Open
eendebakpt
wants to merge
6
commits into
numpy:main
Choose a base branch
from
eendebakpt:broadcast
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+17
−16
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39ff8a4
ENH: Improve performance of np.broadcast_arrays
eendebakpt 1d5beed
modify tests
eendebakpt 4f348ca
lint
eendebakpt d598427
whitespace
eendebakpt ae0c151
lint
eendebakpt 92b9cc9
improve performance of broadcast_shapes
eendebakpt File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -478,7 +478,7 @@ def broadcast_shapes(*args): | |
>>> np.broadcast_shapes((6, 7), (5, 6, 1), (7,), (5, 1, 7)) | ||
(5, 6, 7) | ||
""" | ||
arrays = [np.empty(x, dtype=[]) for x in args] | ||
arrays = [np.empty(x, dtype=bool) for x in args] | ||
return _broadcast_shape(*arrays) | ||
|
||
|
||
|
@@ -546,13 +546,12 @@ def broadcast_arrays(*args, subok=False): | |
# return np.nditer(args, flags=['multi_index', 'zerosize_ok'], | ||
# order='C').itviews | ||
|
||
args = tuple(np.array(_m, copy=None, subok=subok) for _m in args) | ||
args = [np.array(_m, copy=None, subok=subok) for _m in args] | ||
|
||
shape = _broadcast_shape(*args) | ||
|
||
if all(array.shape == shape for array in args): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a nice catch. |
||
# Common case where nothing needs to be broadcasted. | ||
return args | ||
result = [array if array.shape == shape | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the above, I wonder if it is actually slower to directly |
||
else _broadcast_to(array, shape, subok=subok, readonly=False) | ||
for array in args] | ||
return tuple(result) | ||
|
||
return tuple(_broadcast_to(array, shape, subok=subok, readonly=False) | ||
for array in args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this really help? I thought that these days list comprehension and creating a tuple via an iterator made very little speed difference, and
*args
should be very slightly faster for a tuple.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does help, although you are right the cost of list comprehensions has gone down. On Python 3.12:
So about 200ns for a small size, which does matter in the benchmarks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I confirm this. It is funny because I really thought python had solved the speed difference, but clearly I was wrong.