我应该预分配一个numpy数组吗? [英] Should I preallocate a numpy array?

查看:113
本文介绍了我应该预分配一个numpy数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它是方法.该方法在执行过程中重复多次.此方法使用numpy数组作为临时缓冲区.我不需要在方法调用之间的缓冲区中存储值.我是否应该创建数组的成员实例,以避免在方法执行过程中内存分配上的时间泄漏?我知道,最好使用局部变量.但是,Python是否足够聪明,只需为该数组分配一次内存?

I have a class and it's method. The method repeats many times during execution. This method uses a numpy array as a temporary buffer. I don't need to store values inside the buffer between method's calls. Should I create a member instance of the array to avoid time leaks on memory allocation during the method execution? I know, that it is preferred to use local variables. But, is Python smart enough to allocate memory for the array only once?

class MyClass:
    def __init__(self, n):
        self.temp = numpy.zeros(n)
    def method(self):
        # do some stuff using self.temp

class MyClass:
    def __init__(self, n):
        self.n = n
    def method(self):
        temp = numpy.zeros(self.n)
        # do some stuff using temp

更新:用零替换为空

推荐答案

是的,您需要预分配大型数组.但这是否有效取决于您如何使用这些数组.

Yes, you need to preallocate large arrays. But if this will be efficient depends on how you use these arrays then.

这将导致针对中间计算结果的一些新分配:

This will cause several new allocations for intermediate results of computation:

self.temp = a * b + c

这不会(如果预先分配了self.x):

This will not (if self.x is preallocated):

numpy.multiply(a, b, out=self.x)
numpy.add(c, self.x, out=self.temp)

但是对于这些情况(当您以不平凡的公式处理大型数组时)最好使用 numexpr einsum 用于矩阵计算.

But for these cases (when you work with large arrays in not-trivial formulae) it is better to use numexpr or einsum for matrix calculations.

这篇关于我应该预分配一个numpy数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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