What is faster for creatin a matrix, using np.hstack (or np.vstack) inside a for loop, or changing the columns of a previously created np.zeros array?

Viewed 233

I am trying to create a matrix inside a for loop (one column at a time). Which is the faster way to do it? creating a np.empty() array and using np.hstack() to append the columns, or creating a np.zeros() array of the desired shape and changing the columns of the array.

TIA

1 Answers

np.zeros() be faster when the size is big enough that the array operations are significant.

The reason for that is that numpy arrays are stored continuously in one big block of memory. Allocating memory for the array takes time. When using hstack you continuously need to grow the memory for the array, which means re-allocating the memory and copying all the data to the new place, making this essentially an O(n^2) operation. Pre-allocating with np.zeros saves you this time.

Related