Array Storage and Indexing

Most modern computer languages (except Fortran) use row-major order, in which elements of a matrix row are adjacent in memory: A[i][j] is stored as:


0 1 2 ... n
0 1 2 ... n
0 1 2 ... n

To get the best performance, j should be the index of the inner loop, so that j will vary fastest and accesses will be adjacent:

  for (i=0; i<1000; i++)
    for (j=0; j<1000; j++)
      sum = sum + arr[i][j];

Contents    Page-10    Prev    Next    Page+10    Index