[Numpy-discussion] array.sum() slower than expected along some array axes?
Sat Feb 3 20:12:39 CST 2007
On 2/3/07, Robert Kern <email@example.com> wrote:
> Keith Goodman wrote:
> > On 2/3/07, Stephen Simmons <firstname.lastname@example.org> wrote:
> >> Does anyone know why there is an order of magnitude difference
> >> in the speed of numpy's array.sum() function depending on the axis
> >> of the matrix summed?
> >> To see this, import numpy and create a big array with two rows:
> >> >>> import numpy
> >> >>> a = numpy.ones([2,1000000], 'f4')
> >> Then using ipython's timeit function:
> >> Time (ms)
> >> sum(a) 20
> >> a.sum() 9
> >> a.sum(axis=1) 9
> >> a.sum(axis=0) 159
> >> numpy.dot(numpy.ones(a.shape, a.dtype), a) 15
> >> This last one using a dot product is functionally equivalent
> >> to a.sum(axis=0), suggesting that the slowdown is due to how
> >> indexing is implemented in array.sum().
> > I don't know how much time this would account for, but a.sum(0) has to
> > create a much larger array than a.sum(1) does.
> However, so does sum(a) and numpy.dot().
The speed difference across axis 0 and 1 is also seen in Octave and
Matlab (but it is more like a factor of 5). But in those languages
axis=0 is much faster. And numpy, if I remember, stores arrays in the
opposite way as Octave (by row or column, I forget).
So a lot of the speed difference could be in how the array is stored.
More information about the Numpy-discussion