title: Speed up nested for loops and improve performance
body: <p>I am working on a program and it has a pretty long execution time. I'am trying to improve performance where I can, however my knowledge is limited in this area. Can anyone recommend a way to speed up the method below?</p>
  
  <pre><code>public static double DistanceBetween2Points(double[,] p1, double[,] p2, int patchSize)
  {
      double sum = 0;
      for (int i = 0; i &lt; patchSize; i++)
      {
          for (int j = 0; j &lt; patchSize; j++)
          {
              sum += Math.Sqrt(Math.Pow(p1[i, j] - p2[i, j], 2));
          }
      }
      return sum;
  }
  </code></pre>
  
  <p>The method calculates the <em>distance</em> between two images by calculating the sum of all the distances between two points on the two images. </p>