Skip to content

Memory requirement of ak.sum vs np.sum #2480

Answered by jpivarski
alexander-held asked this question in Q&A
Discussion options

You must be logged in to vote

I had naively assumed thus far that awkward just hands this off to numpy and would essentially act the same otherwise.

It's the other way around: if np.sum is called on an ak.Array, it checks to see if ak.Array has a __array_function__, which it does, and then NumPy calls that instead of its usual np.sum operation.

What you're doing here is calling ak.sum on a NumPy array, which converts the NumPy array into an Awkward Array (which is not expensive) and then does general variable-length reduction on that Awkward Array (which is expensive). There are more intermediate arrays involved in ak.sum, and that will account for more memory.

We could install a check in all of the NumPy functions …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@alexander-held
Comment options

@jpivarski
Comment options

Answer selected by alexander-held
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants