[Numpy-discussion] Array concatenation performance

John Porter jporter@cambridgesys....
Thu Jul 15 04:54:53 CDT 2010


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
concatenation.

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:
   numpy.concatenate([a,b,c]).reshape(3,-1)
   numpy.concatenate([[a],[b],[c]))

Is there a better way to efficiently create the array ?

See the following snippet:
---------------------------------------
import time
import numpy as nx
print 'numpy version', nx.version.version
t = time.time()
# test array
a = nx.arange(1000*1000)
print 'a ',time.time()-t
t = time.time()
# create array in the normal way..
b0 = nx.array([a,a,a])
print 'b0',time.time()-t
t = time.time()
# create using empty array
b1 = nx.empty((3,)+(a.shape))
b1[0] = a
b1[1] = a
b1[2] = a
print 'b1',time.time()-t
print nx.all((b0==b1))
-----------------------------------------
Produces the output:
  numpy version 1.3.0
  a  0.0019519329071
  b0 0.286643981934
  b1 0.0116579532623
  equal True


More information about the NumPy-Discussion mailing list