[Numpy-discussion] In-place operations
Johannes Loehnert
a.u.r.e.l.i.a.n at gmx.net
Wed Sep 13 03:23:50 CDT 2006
Hi,
one word in advance, instead of optimizing it is advisable to seek for a way
to refactorize the algorithm using smaller arrays, since this kind of
optimization almost certainly reduces readability. If you do it, comment
well. ;-)
If you have very large arrays and want to do some arithmetics on it, say
B = 2*B + C
you can use inplace operators to avoid memory overhead:
B *= 2
B += C
Another trick which works in most situations is to do the outermost loop in
python:
for i in xrange(len(B)):
B[i] = 2*B[i] + C[i]
This reduces the temporary array size to 1/len(B) while still being fast (if
the other dimensions are large enough). For very large 1d arrays, you could
split them into chunks of a certain size.
However, you have to be careful that your calculation does not access
already-calculated elements of B. Consider the following example:
In [2]: B=arange(10)
In [3]: B+B[::-1]
Out[3]: array([9, 9, 9, 9, 9, 9, 9, 9, 9, 9])
In [4]: B += B[::-1]
In [5]: B
Out[5]: array([ 9, 9, 9, 9, 9, 14, 15, 16, 17, 18])
Johannes
More information about the Numpy-discussion
mailing list