[Numpy-discussion] optimizing ndarray.__setitem__

Christoph Groth cwg at falma.de
Thu May 5 03:29:23 EDT 2011


> On Wed, May 4, 2011 at 6:19 AM, Christoph Groth <cwg at falma.de> wrote:
>>
>>     Dear numpy experts,
>>    
>>     I have noticed that with Numpy 1.5.1 the operation
>>    
>>     m[::2] += 1.0
>>    
>>     takes twice as long as
>>    
>>     t = m[::2]
>>     t += 1.0

Mark Wiebe <mwwiebe at gmail.com> writes:

> You'd better time this in 1.6 too. ;)
>
> https://github.com/numpy/numpy/commit/f60797ba64ccf33597225d23b893b6eb11149860

This seems to be exactly what I had in mind.  Thanks for finding this.

> The case of boolean mask indexing can't benefit so easily from this
> optimization, but I think could see a big performance benefit if
> combined __index__ + __i<op>__ operators were added to
> Python. Something to consider, anyway.

Has something like __index_iadd__ ever been considered seriously?  Not
to my (limited) knowledge.

Indeed, the second loop executes twice as fast than the first in the
following example (again with Numpy 1.5.1).

import numpy
m = numpy.zeros((1000, 1000))
mask = numpy.arange(0, 1000, 2, dtype=int)

for i in xrange(40):
    m[mask] += 1.0

for i in xrange(40):
    t = m[mask]
    t += 1.0

But wouldn't it be easy to optimize this as well, by not executing
assignments where the source and the destination is indexed by the same
mask object?  This would be a bit weaker, as it would work only for same
("a is b"), and not for equal masks, but it should still cover the most
common case.

Christoph




More information about the NumPy-Discussion mailing list