ufunc.at (and possibly other methods) slow
See original GitHub issueI noticed that in many of my codes, seemingly harmless lines like
numpy.add.at(target, idx, vals)
take a large share of the runtime. I investigated and found that one gets a speed-up of a factor of 40 (!) by simply moving the critical code to C++.
(Not sure what’s actually done in numpy.ufunc
.) I put this into a very simple module, https://github.com/nschloe/fastfunc, so feel free to take some code out of there.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Numpy multidimensional indexing for np.ufunc.at and np.ix_ ...
I would like to know how I can take index from an array and multiply with another array. I have two 4d arrays...
Read more >Universal functions (ufunc) — NumPy v1.24 Manual
The Python function max() will find the maximum over a one-dimensional array, but it will do so using a slower sequence interface. The...
Read more >Universal functions (ufunc) — NumPy v1.13 Manual
If an input has a dimension size of 1 in its shape, the first data entry in that dimension will be used for...
Read more >Computation on NumPy Arrays: Universal Functions
Computation on NumPy arrays can be very fast, or it can be very slow. ... Another common type of operation available in a...
Read more >Cloud statistics — How to EUREC⁴A
Here are some ideas which show how this can be done in a vectorized (i.e. numpy friendly) way. This chapter assumes that a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here I explained what makes
ufunc.at
slow. Your code here takes a few shortcuts that are not really available to NumPy:You have hardcoded the operations you have implemented,
+
,-
,*
and/
, which is OK for most use cases, but does not support generic operations. If you had a generic function that took a function pointer that performed the operation, you would have more generic, albeit slower code. That is what NumPy does.You have assumed that the
a
andb
arrays are of the same type, which is not always the case. Not sure if your code can handle cases where these don’t match, but that’s a liberty NumPy can’t take either.I’m not sure how much magic pybind11 packs, but your iteration scheme seems deceptively simple for all the subtleties of fancy indexing that
ufunc.at
supports. Are you assuming that some/all of the arrays are 1D? That’s again something NumPy isn’t free to do.It would be great if we could start using C++ template instead of the
.c.src
stuff within numpy, but I imagine there are distribution issues with that…