Replies: 1 comment
-
Cython tutorial |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Ideas for further performance improvements.
See my python_performance repo for demos of these notes.
Numpy performance considerations
Vectorization and broadcasting are fundamental techniques for using Numpy in a performant manner. This PaperspaceBlog post taught me the foundation of this.
A major consideration when dealing with Numpy arrays is whether the array in an expression is or needs to be copied. This extra step can be time consuming and Numpy has infrastructure to avoid it when possible.
Array copies
Forcing a copy is often done accidentally. For example, the first expression below forces a copy of the array by including
[:]
while the second doesn't.The second is about 20% faster than the first.
This SO post is relevant: https://stackoverflow.com/questions/15424211/transfer-ownership-of-numpy-data
Numpy arrays are sometimes a View to the array. This sounds and acts like a pointer, but it may be something else at the low level
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.transpose.html#numpy.ndarray.transpose
https://www.w3schools.com/python/numpy/numpy_copy_vs_view.asp
Array access
Array access should be contiguous
No element-wise access is better - let Numpy do the multiplication
Beta Was this translation helpful? Give feedback.
All reactions