Search code examples
cperformancefor-loopcpu-cache

Which ordering of nested loops for iterating over a 2D array is more efficient


Which of the following orderings of nested loops to iterate over a 2D array is more efficient in terms of time (cache performance)? Why?

int a[100][100];

for(i=0; i<100; i++)
{
   for(j=0; j<100; j++)
   {
       a[i][j] = 10;    
   }
}

or

for(i=0; i<100; i++)
{
   for(j=0; j<100; j++)
   {
      a[j][i] = 10;    
   }
}

Solution

  • The first method is slightly better, as the cells being assigned to lays next to each other.

    First method:

    [ ][ ][ ][ ][ ] ....
    ^1st assignment
       ^2nd assignment
    [ ][ ][ ][ ][ ] ....
    ^101st assignment
    

    Second method:

    [ ][ ][ ][ ][ ] ....
    ^1st assignment
       ^101st assignment
    [ ][ ][ ][ ][ ] ....
    ^2nd assignment