Fastest way to grow a numpy numeric array

后端 未结 5 2131
后悔当初
后悔当初 2020-11-27 12:01

Requirements:

  • I need to grow an array arbitrarily large from data.
  • I can guess the size (roughly 100-200) with no guarantees that the array will fit
5条回答
  •  Happy的楠姐
    2020-11-27 12:27

    there is a big performance difference in the function that you use for finalization. Consider the following code:

    N=100000
    nruns=5
    
    a=[]
    for i in range(N):
        a.append(np.zeros(1000))
    
    print "start"
    
    b=[]
    for i in range(nruns):
        s=time()
        c=np.vstack(a)
        b.append((time()-s))
    print "Timing version vstack ",np.mean(b)
    
    b=[]
    for i in range(nruns):
        s=time()
        c1=np.reshape(a,(N,1000))
        b.append((time()-s))
    
    print "Timing version reshape ",np.mean(b)
    
    b=[]
    for i in range(nruns):
        s=time()
        c2=np.concatenate(a,axis=0).reshape(-1,1000)
        b.append((time()-s))
    
    print "Timing version concatenate ",np.mean(b)
    
    print c.shape,c2.shape
    assert (c==c2).all()
    assert (c==c1).all()
    

    Using concatenate seems to be twice as fast as the first version and more than 10 times faster than the second version.

    Timing version vstack  1.5774928093
    Timing version reshape  9.67419199944
    Timing version concatenate  0.669512557983
    

提交回复
热议问题