I recently came across this example while reading some introduction to C text. There was no explanation to why the codes below are different when it comes to cache friendliness, and I can't figure out the difference. The text goes like this:
Comparing these two codes:
// cache-friendly
int i, j;
for (i = 0; i < 10; ++i)
{
for (j = 0; j < 20; ++j)
{
a[i][j] += 10;
}
}
VS
// not cache-friendly
int i, j;
for (j = 0; j < 20; ++j)
{
for (i = 0; i < 10; ++i)
{
a[i][j] += 10;
}
}
Does anybody see why code A is cache-friendly compared to code B?