Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improving cache performance while iterating through a simple 2D array?

Tags:

java

c

caching

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;
like image 216
AnchovyLegend Avatar asked Feb 19 '23 08:02

AnchovyLegend


2 Answers

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.

like image 164
JB Nizet Avatar answered Feb 20 '23 23:02

JB Nizet


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.

like image 21
tmyklebu Avatar answered Feb 20 '23 21:02

tmyklebu