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

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion

Reply via email to