I recently ran across a comment which is as follows:
Multi-dimensional arrays take a lot of time for array access. To increase their caching and access speed, it is advised to keep the indices from smaller to larger i.e. declaring
rmq
array likermq[11][11][1002][1002]
rather thanrmq[1002][1002][11][11]
.
I tried a code to test the same. In Code 1:
int pre_compute[18][100001]; //global variable
int main(){
/*some precomputations on pre_compute array
of the order of size of the array*/
/*Run 10^8 queries on pre_compute array,
O(1) per query.*/
}
Code 2:
int pre_compute[100001][18]; //global variable
int main(){
/*exact same precomputations on pre_compute array as done in Code 1
of the order of size of the array*/
/*Run 10^8 queries on pre_compute array,
O(1) per query.*/
}
Both the codes are identical except for the distribution of the array. Still there was a big execution time difference between the two codes. The first code took an average of 0.40 secs
on my PC, whereas the other code took an average of 1.42 secs
.
What can be a possible reason for such a big execution time difference between two implementations of an array?