Sean Mackesey
Sean Mackesey

Reputation: 10939

Performance difference between filling existing numpy array and creating a new one

In iterative algorithms, it is common to use large numpy arrays many times. Frequently the arrays need to be manually "reset" on each iteration. Is there a performance difference between filling an existing array (with nans or 0s) and creating a new array? If so, why?

Upvotes: 6

Views: 2239

Answers (1)

Daniel
Daniel

Reputation: 42748

The answer depends on the size of your arrays. While allocating a new memory region takes nearly a fixed amount of time, the time to fill this memory region grows linear with size. But, filling a new allocated memory with numpy.zeros is nearly twice as fast, as filling an existing array with numpy.fill, and three times faster than item setting x[:] = 0.

So on my machine, filling vectors with less than 800 elements is faster than creating new vectors, with more than 800 elements creating new vectors gets faster.

Upvotes: 6

Related Questions