title: .NET Compiler -- Are there nested loop optimizations built-in?
body: <p>Consider the following code snippet, a traversal of a three dimensional array of singles, in terms of execution efficiency, assuming that <code>process1()</code> and <code>process2()</code> take identical lengths of time to execute:</p>
  
  <pre><code>float arr[mMax,nMax,oMax];
  
  for (m = 0; m &lt; mMax; m++)
    for (n = 0; n &lt; nMax; n++)
      for (o = 0; o &lt; oMax; o++)
        { process1(arr[m,n,o]); }
  
  for (o = 0; o &lt; oMax; o++)
    for (n = 0; n &lt; nMax; n++)
      for (m = 0; m &lt; mMax; m++)
        { process2(arr[m,n,o]); }
  </code></pre>
  
  <p>Now, it's known that C# organizes arrays in the .NET framework as row-major structures. Without any optimization I would assume that the first loop will execute much faster than the second one.</p>
  
  <p>The question is: Does the CLR's JIT or the cs.exe/vb.exe compilers detect and optimize loops like this, perhaps reordering the nesting, or should I always be on my guard for potential performance hits, especially in terms of what might happen if I tried to parallelize the loops? </p>