title: Will matrix multiplication using for loops decrease performance?
body: <p>Currently I'm working on a program that uses matrices. I came up with this nested loop to multiply two matrices:</p>
  
  <pre><code>// The matrices are 1-dimensional arrays
  for (int i = 0; i &lt; 4; i++)
      for (int j = 0; j &lt; 4; j++)
          for (int k = 0; k &lt; 4; k++)
              result[i * 4 + j] += M1[i * 4 + k] * M2[k * 4 + j];
  </code></pre>
  
  <p>The loop works. My question is: will this loop be slower compared to writing it all out manually like this:</p>
  
  <pre><code>result[0] = M1[0]*M2[0] + M1[1]*M2[4] + M1[2]*M2[8] + M1[3]*M2[12];
  result[1] = M1[0]*M2[1] + M1[1]*M2[5] + M1[2]*M2[9] + M1[4]*M2[13];
  result[2] = ... etc.
  </code></pre>
  
  <p>Because in the nested loop, the array positions are calculated and in the second method, they do not.</p>
  
  <p>Thanks.</p>