Now I'm tying to follow the below subject
Performance of 2-dimensional array vs 1-dimensional array
especially, when I assign in my code.cpp code.
actually the below method terribly slow then just mapping
1
int getIndex(int row, int col) return row*NCOLS+col;
#define NROWS 10
#define NCOLS 20
This:
int main(int argc, char *argv[]) {
int myArr[NROWS*NCOLS];
for (int i=0; i<NROWS; ++i) {
for (int j=0; j<NCOLS; ++j) {
myArr[getIndex(i,j)] = i+j;
}
}
return 0;
}
than
2
#define NROWS 10
#define NCOLS 20
This:
int main(int argc, char *argv[]) {
int myArr[NROWS*NCOLS];
for (int i=0; i<NROWS; ++i) {
for (int j=0; j<NCOLS; ++j) {
myArr[row*NCOLS+col] = i+j;
}
}
return 0;
}
But i can't understand it why does '1' slower than '2'?
in experimentally, '1' is slower alomst twice time than '2'. I think this is not makes sense.