numpy函数中参数'out'的实用程序 [英] Utility of parameter 'out' in numpy functions

查看:315
本文介绍了numpy函数中参数'out'的实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

out参数在某些numpy函数(例如cumsumcumprod)或其他数学函数中的用途是什么?

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

如果结果很大,使用out参数有助于改善计算时间或内存效率吗?

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?

推荐答案

采用out参数的函数将创建新对象.这通常是您从该函数中期望的结果:提供一些数组,并使用转换后的数据获得一个新数组.

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.

在这种情况下,您可能要创建一个输出数组out,然后让该函数用输出填充该数组.处理完数据后,您可以重用out并让函数覆盖其值.这样,您将不会分配或释放任何内存,从而可以节省大量时间.

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.

这篇关于numpy函数中参数'out'的实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆