'Out' parameter utility in numpy function

What is the usefulness of the out parameter in certain numpy functions like cumsum or cumprod or other math functions?

If the result is huge, does it help to use the out parameter to increase computational time or memory efficiency?

This thread gives some information on how to use it. But I want to know when to use it and what may be the benefits?

+7
performance python algorithm numpy parameters
source share
1 answer

Functions that take the out parameter create new objects. Usually this is what you expect from a function: Give some array and get a new one with the converted data.

However, imagine that you want to call this function several thousand times in a row. Each function call creates a new array, which, of course, takes a lot of time.

In this case, you can create an output array out and let the function fill the array with output. After processing the data, you can reuse out and let the function overwrite your values. This way you will not allocate or free up any memory, which can save you a lot of time.

+6
source share

All Articles