[Numpy-discussion] question about in-place operations

Massimo DiPierro massimo.dipierro at gmail.com
Tue May 22 10:54:40 EDT 2012

For now I will be doing this:

import numpy
import time


# naive solution
t0 = time.time()
for i in xrange(len(a)):
    a[i] += c*b[i]
print time.time()-t0

# possible solution
t0 = time.time()
for i in xrange(0,len(a),n):
    a[i:i+n] += c*b[i:i+n]
print time.time()-t0

the second "possible" solution appears 1000x faster then the former in my tests and uses little extra memory. It is only 2x slower than b*=c.

Any reason not to do it?

On May 22, 2012, at 9:32 AM, Dag Sverre Seljebotn wrote:

> On 05/22/2012 04:25 PM, Massimo DiPierro wrote:
>> hello everybody,
>> first of all thanks to the developed for bumpy which is very useful. I am building a software that uses numpy+pyopencl for lattice qcd computations. One problem that I am facing is that I need to perform most operations on arrays in place and I must avoid creating temporary arrays (because my arrays are many gigabyte large).
>> One typical operation is this
>> a[i] += const * b[i]
>> What is the efficient way to do is when a and b are arbitrary arrays?  const is usually a complex number.
>> a and b have the same shape but are not necessarily uni-dimensional.
> I don't think NumPy support this; if you can't modify b[i] in-place, I 
> think your only option will be one of numexpr/Theano/Cython.
> Dag
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list