[prev] 42 [next]

Exercise 4: Reference Locality

Consider the following data structure

typedef struct { int acc[3]; int vel[3]; } Point
Point p[10000];

and two functions to clear all the values in the array.

Discuss the locality of data reference in each of these functions:

void clear1(point *p, int n)
{
   int i,j;
   for (i = 0; i < n; i++) {
      for (j = 0; j < 3; j++) p[i].acc[j] = 0;
      for (j = 0; j < 3; j++) p[i].vel[j] = 0;
   }
}
void clear2(point *p, int n)
{
   int i,j;
   for (j = 0; j < 3; j++) {
      for (i = 0; i < n; i++) p[i].acc[j] = 0;
      for (i = 0; i < n; i++) p[i].vel[j] = 0;
   }
}

[Diagram:Pics/opsys/points-array.png]