Code A for int v 0 v 16000 v for int c 0 c 200 c for i

//Code A:
for (int v = 0; v < 16000; v++) {
  for (int c = 0; c < 200; c++) {
          for (int r = 0; r < 24; r++) {
              total = array[r][v][c];
          }
}
}


//Code B:
for (int c = 0; c < 200; c++) {
  for (int r = 0; r < 24; r++) {
      for (int v = 0; v < 16000; v++) {
          total = array[r][v][c];
      }
  }
}

//Code C:
for (int r = 0; r < 24; r++) {
  for (int v = 0; v < 16000; v++) {
      for (int c = 0; c < 200; c++) {
          total = array[r][v][c];
      }
  }
}

Which one of the following code from best to worst exhibits the best use of spatial locality?

Select one answer:

1. A, C, B
2. A, 2, C
3. B, A, C
4. C, B, A
5. C, A, B

Solution

In all the codes, there are 24 x 16000 x 200 cells.
//Code A:
for (int v = 0; v < 16000; v++) {
for (int c = 0; c < 200; c++) {
for (int r = 0; r < 24; r++) {
total = array[r][v][c];
}
}
}
This code is the one with the worst spatial locality.
It can be assumed like a 16000 planes, where in each plane, is of size 200 x 24 rectangle.

//Code B:
for (int c = 0; c < 200; c++) {
for (int r = 0; r < 24; r++) {
for (int v = 0; v < 16000; v++) {
total = array[r][v][c];
}
}
}
This code will have inbetween spatial locality.
It can be assumed like 200 planes, where in each plane, is of size 24 x 16000 rectangle.


//Code C:
for (int r = 0; r < 24; r++) {
for (int v = 0; v < 16000; v++) {
for (int c = 0; c < 200; c++) {
total = array[r][v][c];
}
}
}
This code is the one with the first best spatial locality.
It can be assumed like a tightly packed cuboid with 24 width, 16000 length, and 200 height.

Which one of the following code from best to worst exhibits the best use of spatial locality?
Select one answer:
1. A, C, B
2. A, 2, C
3. B, A, C
4. C, B, A
5. C, A, B
Therefore, the answer is: 5. C, A, B.

//Code A: for (int v = 0; v < 16000; v++) { for (int c = 0; c < 200; c++) { for (int r = 0; r < 24; r++) { total = array[r][v][c]; } } } //Code B: for
//Code A: for (int v = 0; v < 16000; v++) { for (int c = 0; c < 200; c++) { for (int r = 0; r < 24; r++) { total = array[r][v][c]; } } } //Code B: for

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site