Skip to content Skip to sidebar Skip to footer

Should I Preallocate A Numpy Array?

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

Solution 1:

Numpy arrays are fast, once created. However, creating an array is pretty expensive. Much more than, say, creating a python list.

In a case such as yours, where you create a new array again and again (in a for loop?), I would ALWAYS pre-allocate the array structure and then reuse it.

I can't comment on whether Python is smart enough to optimize this, but I would guess it's not :)

How big is your array and how frequent are calls to this method?

Solution 2:

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

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

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

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.

Post a Comment for "Should I Preallocate A Numpy Array?"