0
$\begingroup$

C++ code:

int main()
{
short int arr[4][4];
for (int i = 0; i < 8; i++)
{
  for (int j = 0; j < 8; j++)
  {
    arr[i][j] = i+j;
  }
}
return 0;
}

Is there going to be a difference in hit time if it is loading data from arrays or storing data into array. For example for arr[i][j] = i+j; and a = arr[i][j]; ?

Also what is difference in hit time if it is arr[j][i] = i+j;

$\endgroup$
7
  • $\begingroup$ Since the array is quite short and cache is often done in blocks afaik, isn't it likely that the entire array gets cached, so every access after the first is a hit? $\endgroup$
    – user136782
    Dec 2, 2023 at 15:14
  • $\begingroup$ Yes, is it going to be the same for arr[j][i] = i+j ? $\endgroup$
    – Luke
    Dec 2, 2023 at 15:57
  • $\begingroup$ I am not really seeing any reason for it to be different $\endgroup$
    – user136782
    Dec 2, 2023 at 16:00
  • $\begingroup$ Okay, so when we are storing the data to the array, the hit time for both arr[j][i] and arr[i][j] would be same ? But when we are loading the data from the array. It should be different $\endgroup$
    – Luke
    Dec 2, 2023 at 16:07
  • $\begingroup$ If the entire array is cached, by that i mean the block containing the entire array is brought into cache on the first access, then all subsequent access should take the same time. $\endgroup$
    – user136782
    Dec 2, 2023 at 16:13

1 Answer 1

1
$\begingroup$

Your program will most likely crash because you access non-existing array elements.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.