I have a class and its method. The method is repeated many times at runtime. This method uses an array numpy
as a temporary buffer. I do not need to store values inside the buffer between method calls. Should I create an instance member of the array to avoid time leaks when allocating memory during method execution? I know it is preferable to use local variables. But, is it enough for Python to allocate memory for the array only once?
class MyClass:
def __init__(self, n):
self.temp = numpy.zeros(n)
def method(self):
or
class MyClass:
def __init__(self, n):
self.n = n
def method(self):
temp = numpy.zeros(self.n)
Update: replace empty with zeros
source
share