[Numpy-discussion] Optimize speed of for loop using numpy

Trond Kristiansen trond@unc....
Mon Feb 25 20:08:16 CST 2008

Hi all.
This is my first email to the discussion group. I have spent two days trying
to get a particular loop to speed up, and the best result I got was this:



for i in range(xi):

for j in range(eta):

for k in range(s):

if z_r[k,j,i] < depth:

if tmp1[j,i]==10000:

if (depth - z_r[k,j,i]) <= (depth - z_r[0,j,i]):

if (depth - z_r[k,j,i]) <= (depth - z_r[int(tmp1[j,i]),j,i]):

elif z_r[k,j,i] >= depth:

if tmp2[j,i]==10000:

if abs(depth - z_r[k,j,i]) <= abs(depth - z_r[s-1,j,i]) :

if abs(depth - z_r[k,j,i]) <= abs(depth - z_r[int(tmp2[j,i]),j,i]) :


Not very impressive. My problem is that I can not find any numpy functions
that actually can do the tests I do for each k value in the arrays. I need
to identify the position (i,j) of k-values that meet the specified
requirement. There are way too many if-else tests here as well. The scripts
takes about 10 seconds to run (for 238MB input file), but these arrays are
read from netCDF files and can be much larger and easily grow to enormous
dimensions. It is therefore crucial for me to speed things up. Hope some of
you can help. I really appreciate all feedback on this. I am just a rooky to

Cheers and thanks for all help, Trond

More information about the Numpy-discussion mailing list