title: Horizontal Vs Vertical browsing of array
body: <p>Lets consider two almost identical codes:</p>
  
  <p>First  </p>
  
  <pre><code>for (int k=0;k&lt;1000;k++)
  {
      for (int i=0;i&lt;600;i++)
      {
           for (int j=0;j&lt;600;j++)
           {
                tab[i][j] = i *j;
           }
      }
   }
  </code></pre>
  
  <p>Second</p>
  
  <pre><code>for (int k=0;k&lt;1000;k++)
  {
      for (int i=0;i&lt;600;i++)
      {
           for (int j=0;j&lt;600;j++)
           {
                tab[j][i] = i *j;
           }
      }
   }
  </code></pre>
  
  <p>In the second one instead of tab[i][j] we have tab[j][i].<br>
  The first code is much faster.</p>
  
  <p><strong>Question</strong><br>
  Why is the first code much faster?  </p>
  
  <p><strong>My intuition</strong><br>
  Is it because when a program tries to access a cell , firstly whole block that contains this cell is move to the cache, and then it is accessed via cache. Since array in memory is represented by consecutive cells, then in first case then in first case there is much less access to the memory than in the second case.</p>