Contents    Page-10    Prev    Next    Page+10    Index   

Does Array Order Matter?


static double arr[1000][1000];

double test1 ()
{ double sum; int i, j;
  sum = 0.0;
  for (i=0; i<1000; i++)
    for (j=0; j<1000; j++)
      sum = sum + arr[i][j];
  return (sum); }

double test2 ()
{ double sum; int i, j;
  sum = 0.0;
    for (j=0; j<1000; j++)
  for (i=0; i<1000; i++)
      sum = sum + arr[i][j];
  return (sum); }

The two programs compute the same result, but performance is quite different:


test1 result = 1000000.0    time =  430000
test2 result = 1000000.0    time = 1940000