Contents    Page-10    Prev    Next    Page+10    Index   

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

Performance is significantly better if the order of accessing array elements takes advantage of memory locality.

To get the best performance, the last subscript, 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];