title: More efficient way to loop?
body: <p>I have a small piece of code from a much larger script. I figured out that when the function <code>t_area</code> is called, it is responsible for most of the run time. I tested the function by itself, and it is not slow, it takes a lot of time because of the number of times that it has to be ran I believe. Here is the code where the function is called: </p>
  
  <pre><code>tri_area = np.zeros((numx,numy),dtype=float)
  for jj in range(0,numy-1):
      for ii in range(0,numx-1):
        xp = x[ii,jj]
        yp = y[ii,jj]
        zp = surface[ii,jj]
        ap = np.array((xp,yp,zp))
  
        xp = xp+dx
        zp = surface[ii+1,jj]
        bp = np.array((xp,yp,zp))
  
        yp = yp+dx
        zp = surface[ii+1,jj+1]
        dp = np.array((xp,yp,zp))
  
        xp = xp-dx
        zp = surface[ii,jj+1]
        cp = np.array((xp,yp,zp))
  
        tri_area[ii,jj] = t_area(ap,bp,cp,dp)
  </code></pre>
  
  <p>The size of the arrays in use here are <code>216 x 217</code>, and so are the values of <code>x</code> and <code>y</code>. I am pretty new to python coding, I have used MATLAB in the past. So my question is, is there a way to get around the two for-loops, or a more efficient way to run through this block of code in general? Looking for any help speeding this up! Thanks! </p>
  
  <p>EDIT: </p>
  
  <p>Thanks for the help everyone, this has cleared alot of confusion up. I was asked about the function t_area that is used in the loop, here is the code below: </p>
  
  <pre><code>def t_area(a,b,c,d):
  ab=b-a
  ac=c-a
  tri_area_a = 0.5*linalg.norm(np.cross(ab,ac))
  
  db=b-d
  dc=c-d
  tri_area_d = 0.5*linalg.norm(np.cross(db,dc))
  
  ba=a-b
  bd=d-b
  tri_area_b = 0.5*linalg.norm(np.cross(ba,bd))
  
  ca=a-c
  cd=d-c
  tri_area_c = 0.5*linalg.norm(np.cross(ca,cd))
  
  av_area = (tri_area_a + tri_area_b + tri_area_c + tri_area_d)*0.5
  return(av_area)
  </code></pre>
  
  <p>Sorry for the confusing notation, at the time it made sense, looking back now I will probably change it. Thanks! </p>