[Numpy-discussion] Adding a 2D with a 1D array...

Citi, Luca lciti at essex.ac.uk
Wed Sep 9 07:20:56 EDT 2009


I am sorry but it doesn't make much sense.
How do you measure the performance?
Are you sure you include the creation of the "c" output array in the time spent (which is outside the for loop but should be considered anyway)?

Here are my results...

In [84]: a = np.random.rand(8,26)

In [85]: b = np.random.rand(8)

In [86]: def o(a,b):
   ....:     c = np.empty_like(a)
   ....:     for i in range(len(a)):
   ....:             c[i] = a[i] + b[i]
   ....:     return c
   ....:

In [87]: d = a + b[:,None]

In [88]: (d == o(a,b)).all()
Out[88]: True

In [89]: %timeit o(a,b)
%ti10000 loops, best of 3: 36.8 µs per loop

In [90]: %timeit d = a + b[:,None]
100000 loops, best of 3: 5.17 µs per loop

In [91]: a = np.random.rand(80000,26)

In [92]: b = np.random.rand(80000)

In [93]: %timeit o(a,b)
%ti10 loops, best of 3: 287 ms per loop

In [94]: %timeit d = a + b[:,None]
100 loops, best of 3: 15.4 ms per loop




More information about the NumPy-Discussion mailing list