-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: ndarray.sum() slow compared to + operation for narrow array #22870
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
Comments
Hey, currently working on this! I am looking for the C Source code for np.sum right now. I've been able to track it down to here -will keep looking! |
This goes into the ufunc/umath C-code. NumPy tries to operate on the fastest memory order, but in the case of narrow arrays that isn't the better options, because the memory order doesn't matter that much (probably), and there is a per-call overhead which is much larger NumPy could try to have heuristics to choose the "slow" direction, but also I would have to dig in to see if that is actually easy to find a heuristic (or more importantly, add it in a nice way). |
@dgasmith May have some thoughts on this. |
Hmmm, it seems likely that a large chunk is also due to SIMD overhead for very small loop sizes, making the "fast in memory" choice even worse. This comment suggest that might be easy to improve a lot though, and with that PR merged (or similar), that might just happen. |
PR #22889 will not affect this since this is a reduce, which as far as I can tell does not use SIMD processing (it goes directly to |
Describe the issue:
I ran the following code:
and this was the output:
I.e, the .sum() method was about 6-7X slower than manual sum. If instead I do a slightly less narrow array, say (10**6, 2), .sum() is still 6-7X slower, so this doesn't appear to be just about numpy C-function call overhead.
For more "square" arrays, we actually see the expected improvement:
Are is .sum() needlessly slow for narrow arrays?
Reproduce the code example:
Error message:
No response
Runtime information:
1.21.5
3.9.15 (main, Nov 24 2022, 14:39:17) [MSC v.1916 64 bit (AMD64)]
Context for the issue:
No response
The text was updated successfully, but these errors were encountered: