which way is faster and compiler/cache friendlier, use M[a][b] or M[a*b] when working with matrices?
I tried writing both ways on compiler explorer in a function that allocates, initialises and returns a matrix but I don't know assembly and how much time each instruction takes
int **M = malloc(sizeof(int*)*m)
for(i=0; i<m; ++i) {
*M = malloc(sizeof(int)*n);
for(int j = 0; j < n; ++j){
M[j] = j;
}
vs
int *M = malloc(m*n*sizeof(int));
for(i = 0; i < m*n; ++i) M[i] = i;
I expect the second way to be faster.