[Numpy-discussion] Array concatenation performance
Thu Jul 15 10:15:13 CDT 2010
On Thu, Jul 15, 2010 at 11:05 AM, John Porter <email@example.com> wrote:
> You're right - I screwed up the timing for the one that works...
> It does seem to be faster.
> I've always just built arrays using nx.array() in the past though
> and was surprised
> that it performs so badly.
> On Thu, Jul 15, 2010 at 2:41 PM, Skipper Seabold <firstname.lastname@example.org> wrote:
>> On Thu, Jul 15, 2010 at 5:54 AM, John Porter <email@example.com> wrote:
>>> Has anyone got any advice about array creation. I've been using numpy
>>> for a long time and have just noticed something unexpected about array
>>> It seems that using numpy.array([a,b,c]) is around 20 times slower
>>> than creating an empty array and adding the individual elements.
>>> Other things that don't work well either:
>>> Is there a better way to efficiently create the array ?
>> What was your timing for concatenate? It wins for me given the shape of a.
>> In : import numpy as np
>> In : a = np.arange(1000*1000)
>> In : timeit b0 = np.array([a,a,a])
>> 1 loops, best of 3: 216 ms per loop
>> In : timeit b1 = np.empty(((3,)+a.shape)); b1=a;b1=a;b1=a
>> 100 loops, best of 3: 19.3 ms per loop
>> In : timeit b2 = np.c_[a,a,a].T
>> 10 loops, best of 3: 30.5 ms per loop
>> In : timeit b3 = np.concatenate([a,a,a]).reshape(3,-1)
>> 100 loops, best of 3: 9.33 ms per loop
In : timeit b4 = np.vstack((a,a,a))
100 loops, best of 3: 9.46 ms per loop
More information about the NumPy-Discussion