Utility of parameter 'out' in numpy functions

可紊 提交于 2019-12-12 10:29:06

问题


What is the utility of the out parameter in certain numpy functions such as cumsum or cumprod or other mathematical functions?

If the result is huge in size does it help to use the out parameter to improve computational time or memory efficiency?

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


回答1:


Functions that take the out parameter create new objects. This is usually what you would expect from the function: Give some array and get a new one with the transformed data.

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

In this case you may want to create an output array out and let the function fill the array with the output. After processing the data you can reuse out and let the function overwrite its values. This way you won't allocate or free any memory, which can save you a lot of time.



来源:https://stackoverflow.com/questions/27293830/utility-of-parameter-out-in-numpy-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!