title: optimize 2D array in C++
body: <p>I'm dealing with a 2D array with the following characteristics:</p>
  
  <pre><code>const int cols = 500; 
  const int rows = 100; 
  int arr[rows][cols];
  </code></pre>
  
  <p>I access array arr in the following manner to do some work:</p>
  
  <pre><code>for(int k = 0; k &lt; T; ++k) { // for each trainee
    myscore[k] = 0;
    for(int i = 0; i &lt; cols; ++i) { // for each sample  
      for(int j = 0; j &lt; rows; ++j) { // for each expert
        myscore[k] += delta(i, anotherArray[k][i], arr[j][i]);
      }   
    }
  }
  </code></pre>
  
  <p>So I am worried about the array 'arr' and not the other one. I need to make this more cache-friendly and also boost the speed. I was thinking perhaps transposing the array but I wasn't sure how to do that. My implementation turns out to only work for square matrices. How would I make it work for non-square matrices?</p>
  
  <p>Also, would mapping the 2D array into a 1D array boost the performance? If so, how would I do that? Finally, any other advice on how else I can optimize this... I've run out of ideas, but I know that arr[j][i] is the place where I need to make changes because I'm accessing columns by columns instead of rows by rows so that is not cache friendly at all.</p>
  
  <p>Thanks,
  Hristo</p>