2-d in-place operation performance vs 1-d non in-place
I was surprised to see that an in-place modification of a 2-d array turns out to be slower from the respective non-mutating operation on 1- d arrays, although the latter creates new array objects. Here is the benchmarking code: import timeit for n in 10,100,1000,10000: setup = 'from numpy.random import random;' \ 'm=random((%d,2));' \ 'u1=random(%d);' \ 'u2=u1.reshape((u1.size,1))' % (n,n) timers = [timeit.Timer(stmt,setup) for stmt in # 1-d operations; create new arrays 'a0 = m[:,0]-u1; a1 = m[:,1]-u1', # 2-d in place operation 'm -= u2' ] print n, [min(timer.repeat(3,1000)) for timer in timers] And some results (Python 2.5, WinXP): 10 [0.010832382327921563, 0.0045706926438974782] 100 [0.010882668048592767, 0.021704993232380093] 1000 [0.018272154701226007, 0.19477587235249172] 10000 [0.073787590322233698, 1.9234369172618306] So the 2-d in-place modification time grows linearly with the array size but the 1-d operations are much more efficient, despite allocating new arrays while doing so. What gives ? George
participants (3)
-
Francesc Altet
-
George Sakkis
-
Timothy Hochberg