I have been trying to think of a way to rewrite the code below to improve cache performance ( by reducing misses in cache) in the array.
I am aware that the array is stored in memory row by row (sequentially), so ary[0][0], ary[0][1], ary[0][2],....ary[1][0], ary[1][1], ary[1][2]... ary[50][0], ary[50][1]...ary[50][50]. However, I am uncertain how to use this info to help me figure out how to modify the loop to improve cache performance.
for (c = 0; c < 50; c++)
for (d = 0; d < 50; d++)
ary[d][c] = ary[d][c] + 1;
If you want to access all the cells of a row at once, just inverse the two loops:
for (d = 0; d < 50; d++)
for (c = 0; c < 50; c++)
ary[d][c] = ary[d][c] + 1;
Or even
for (d = 0; d < 50; d++)
int[] array = ary[d];
for (c = 0; c < 50; c++)
array[c] = array[c] + 1;
But I doubt it has any significant impact, or even any impact at all, especially on a so tiny array. Make your code simple and readable. Don't pre-optimize.
Swap the loop order. You're accessing arr[1][0]
right after arr[0][0]
. arr[1][0]
is much farther away, while arr[0][1]
is at the next address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With