[Numpy-discussion] Speed of np.array versus np.vstack

Gael Varoquaux gael.varoquaux at normalesup.org
Wed Oct 14 11:32:07 EDT 2009


I tend to use np.array to stack arrays rather than np.vstack, as I find
it does what I want with higher dimensional arrays. However, I was quite
surprised to see a large speed difference:

In [1]: import numpy as np

In [2]: N = 1e6

In [3]: M = 10

In [4]: l = [np.random.random(N) for _ in range(M)]

In [5]: %timeit np.vstack(l)
10 loops, best of 3: 82.7 ms per loop

In [6]: %timeit np.array(l)
10 loops, best of 3: 822 ms per loop

I can't find the reasons for this speed difference. Also, I don't see
what is the correct way to get the behavior I want without paying the
extra speed cost.

Cheers,

Gaël



More information about the NumPy-Discussion mailing list