Best Practices for Optimizing NumPy Performance in Scientific Python Projects!

Avoiding allocations can be important when considering performance. One technique to use there is to pre-allocate the memory and pass it in to a function with the out parameter.

Back in the day is used to also be important to consider the memory layout of your (multi-dimensional) array as routines often assumed C order (IIRC). These days functions default to K order which tries to adapt to the memory layout of the inputs.

Where non-contiguous memory is unavoidable, there may still be some performance benefit to copying the data to ensure it is contiguous before operating on it.

1 Like