I have a large numpy array that I need to reset its values to 0 regularly. I tried these:
x = np.zeros((10**4, 10**6), dtype=np.float32)
%timeit x[:7000, :] = 0.0 # 4 seconds
%timeit x[:7000, :].fill(0.0) # 4 seconds
On the other hand, creating a new array is much faster:
%timeit x = np.zeros((10**4, 10**6), dtype=np.float32) # 8 microseconds
However, the new array has different memory address that significantly decreases the performance of subsequent copying.
Is there a way to reset the array values to 0 as fast as creating a new array?
Otherwise, is there a way to creating a new zeros array that keeps the same memory address?
source https://stackoverflow.com/questions/69367142/fastest-way-to-reset-a-numpy-array-to-zero
Comments
Post a Comment