Understanding how to write cache compatible code

I was trying to figure out how to write code useful for caching. So, as a first step, I was trying to understand the difference in performance between accessing the main row of an array and basic access to columns.

So, I created an int array of size 512 × 512 so that the total size is 1 MB. My L1 cache is 32 KB, the L2 cache is 256 KB, and the third level cache is 3 MB. So my array is suitable for L3 cache.

I just calculated the sum of the elements of the array in row order and sort order by columns and compared their speed. The main column order is a bit faster all the time. I expected that the main order of the lines would be faster than another (maybe several times faster).

I thought the problem might be due to the small size of the array, so I made another array of size 8192 × 8192 (256 MB). All the same result.

Below is the code snippet that I used:

#include "time.h"
#include <stdio.h>

#define S 512
#define M S
#define N S

int main() {
    // Summing in the row major order
    int x = 0;
    int iter = 25000;
    int i, j;
    int k[M][N];
    int sum = 0;    
    clock_t start, end;

    start = clock();
    while(x < iter) {
        for (i = 0; i < M; i++) {
            for(j = 0; j < N; j++) {
                sum += k[i][j];
            }
        }

        x++;
    }
    end = clock();
    printf("%i\n", end-start);

    // Summing in the column major order
    x = 0;
    sum = 0;
    int h[M][N];

    start = clock();
    while(x < iter) {
        for (j = 0; j < N; j++) {
            for(i = 0; i < M; i++){
                sum += k[i][j];
            }
        }

        x++;
    }
    end = clock();
    printf("%i\n", end-start);
}

Question: can someone tell me what is my mistake and why am I getting this result?

+4
source share
1 answer

I really don't know why you got this behavior, but let me clarify some things.

2 : . , Intel i7 920 256 L2 64 . , , . , , : , , . , , . (, ) - . (, int - 4 ), - ( 64 : 16 ( , ), ). . 1 16 , - ( , , , , , , , , , , ).

, , , , , , , . , , , . , , . : , - , .

Edit

3-4- ( > 8- . ?). [..] , , 3x

, " " , " ", , .

, , -. , , , , , , 3- , ( , L2, , L3). (, 10 ), , , . , . " ", " ", , . , , , " " ( ). , , , " ".

+9

All Articles