[Numpy-discussion] cumsum much slower than simple loop?

josef.pktd@gmai... josef.pktd@gmai...
Thu Feb 9 23:21:46 CST 2012


On Thu, Feb 9, 2012 at 11:39 PM, Dave Cook <daverz@gmail.com> wrote:
> Why is numpy.cumsum (along axis=0) so much slower than a simple loop?  The
> same goes for numpy.add.accumulate
>
> # cumsumtest.py
> import numpy as np
>
> def loopcumsum(a):
>     csum = np.empty_like(a)
>     s = 0.0
>     for i in range(len(a)):
>         csum[i] = s = s + a[i]
>     return csum
>
> npcumsum = lambda a: np.cumsum(a, axis=0)
>
> addaccum = lambda a: np.add.accumulate(a)
>
> shape = (100, 8, 512)
> a = np.arange(np.prod(shape), dtype='f').reshape(shape)
> # check that we get the same results
> print (npcumsum(a)==loopcumsum(a)).all()
> print (addaccum(a)==loopcumsum(a)).all()
>
> ipython session:
>
> In [1]: from cumsumtest import *
> True
> True
>
> In [2]: timeit npcumsum(a)
> 100 loops, best of 3: 14.7 ms per loop
>
> In [3]: timeit addaccum(a)
> 100 loops, best of 3: 15.4 ms per loop
>
> In [4]: timeit loopcumsum(a)
> 100 loops, best of 3: 2.16 ms per loop

strange (if I didn't make a mistake)

In [12]: timeit a.cumsum(0)
100 loops, best of 3: 7.17 ms per loop

In [13]: timeit a.T.cumsum(-1).T
1000 loops, best of 3: 1.78 ms per loop

In [14]: (a.T.cumsum(-1).T == a.cumsum(0)).all()
Out[14]: True

Josef

>
> Dave Cook
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list