I have a class and its 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 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
Or
class MyClass:def __init__(self, n):self.n = ndef method(self):temp = numpy.zeros(self.n)# do some stuff using temp
Update: replaced np.empty
with np.zeros