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

Dave Cook daverz at gmail.com
Thu Feb 9 23:39:57 EST 2012


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

Dave Cook
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120209/572e3647/attachment.html>


More information about the NumPy-Discussion mailing list