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

Hoyt Koepke hoytak@gmail....
Mon Feb 25 20:40:19 CST 2008

```I would definitely suggest using scipy's weave.inline for this.  It
seems like this particular function can be translated into C code
really easily, which would give you a HUGE speed up.  Look at some of
the examples in scipy/weave/examples to see how to do this.  The numpy
book also has a section on it.

One of the reasons I've left matlab and never looked back is how easy
it is to interweave bits of compiled C code for loops like this.

--Hoyt

On Mon, Feb 25, 2008 at 6:32 PM, Trond Kristiansen <trond@unc.edu> wrote:
> Hi again.
>
>  I have attached the function that the FOR loop is part of as a python file.
>  What I am trying to do is to create a set of functions that will read the
>  output files (NetCDF) from running the ROMS model (ocean model). The output
>  file is organized in xi (x-direction), eta (y-direction), and s
>  (z-direction) where the s-values are vertical layers and not depth. This
>  particular function (z_slice) will find the closest upper and lower s-layer
>  for a given depth in meters (e.g. -100). Then values from the two selcted
>  layers will be interpolated to create a new layer at the selected depth
>  (-100). The problem is that the s-layers follow the bathymetry and a
>  particular s-layer will therefore sometimes be above and sometimes below the
>  selected depth that we want to interpolate to. That's why I need a quick
>  script that searches all of the layers and find the upper and lower layers
>  for a given depth value (which is negative). The z_r is a 3D array
>  (s,eta,xi) that is created using the netcdf module.
>
>  The main goal of these set of functions is to move away from using matlab,
>  but also to speed things up. The sliced data array will be plotted using GMT
>  or pyNGL.
>
>  Thanks for helping me. Cheers, Trond
>
>
>
>
>  On 2/25/08 9:15 PM, "Robert Kern" <robert.kern@gmail.com> wrote:
>
>  > On Mon, Feb 25, 2008 at 8:08 PM, Trond Kristiansen <trond@unc.edu> wrote:
>  >>
>  >>
>  >>  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:
>  >
>  > Can you try to repost this in such a way that the indentation is
>  > preserved? Feel free to attach it as a text file. Also, can you
>  > describe at a higher level what it is you are trying to accomplish and
>  > what the arrays mean?
>
>
> _______________________________________________
>  Numpy-discussion mailing list
>  Numpy-discussion@scipy.org
>  http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
```