Improve C function performance with cache locality?

后端 未结 3 1192
小蘑菇
小蘑菇 2020-12-17 00:58

I have to find a diagonal difference in a matrix represented as 2d array and the function prototype is

int diagonal_diff(int x[512][512])

I

3条回答
  •  半阙折子戏
    2020-12-17 01:38

    With one minor change you can have your loops only operate on the desired indices. I just changed the j loop initialization.

    int i, j, result = 0;
    for (i = 0; i < 4; ++i) {
        for (j = i + 1; j < 4; ++j) {
            result += abs(array[i][j] - array[j][i]);
        }
    }
    

提交回复
热议问题