title: Cache Performance (concerning loops) in C
body: <p>I was wondering, why does one set of loops allow for better cache performance than another in spite of logically doing the same thing?</p>
  
  <ol>
  <li><pre><code>for (i = 0; i &lt; n; i++) {
      for (j = 0; j &lt; n; j++) {
          accum = 0.0;
          for (k = 0; k &lt; n; k++) {
              accum += b[j][k] * a[k][i];
          }
          c[j][i] = accum;
      }
  }
  </code></pre></li>
  <li><pre><code>for (j = 0; j &lt; n; j++) {
      for (k = 0; k &lt; n; k++) {
          val = b[j][k];
          for (i = 0; i &lt; n; i++) {
              c[j][i] += val * a[k][i];
          }
      }
  }
  </code></pre></li>
  </ol>
  
  <p>I believe the first one above delivers better cache performance, but why?</p>
  
  <p>Also, when we increase block size, but keep cache size and associativity constant, does it influence the miss rate? At a certain point increasing block size can cause a higher miss rate, right?</p>