[Numpy-discussion] Suggestion for a speed improved accumarray receipe

Michael Löffler ml@redcowmedia...
Mon Feb 18 01:52:23 CST 2013


Hello numpy list!

I had tried to contribute to the AccumarrayLike recipe cookbook, but
access seems to be restricted and no registration allowed. The current
recipe mimics the matlab version in most aspects, but concerning
performance it's horribly slow. 

The following snippet handles only the most simple usecase, but gives
about 16x speed improvement compared to the current receipe, so it would
probably worth to also mention it in the cookbook:


def accum_custom(accmap, a, func=np.sum):
    indices = np.where(np.ediff1d(accmap, to_begin=[1], 
                                  to_end=[np.nan]))[0]
    vals = np.zeros(len(indices) - 1)
    for i in xrange(len(indices) - 1):
        vals[i] = func(a[indices[i]:indices[i+1]])
    return vals


accmap = np.repeat(np.arange(100000), 20)
a = np.random.randn(accmap.size)

%timeit accum(accmap, a, func=np.sum)
>>> 1 loops, best of 3: 16.7 s per loop

%timeit accum_custom(accmap, a, func=np.sum)
>>> 1 loops, best of 3: 945 ms per loop

Best regards,

Michael



More information about the NumPy-Discussion mailing list