Which function is more efficient?
int rows=20000;
int col=30000;
void func1() {
for(i=0;i<rows;i++) {
for(j=0;j<col;j++)
print(a[i][j]);
}
}
void func2() {
for(j=0;j<col;j++) {
for(i=0;i<rows;i++)
print(a[i][j]);
}
}
Performance will probably be better for the first function because the data being accessed is arranged sequentially (or mostly sequentially) in memory. This is faster because doing a read on one memory location will cause adjacent memory locations to be brought into the cache, so when they are read, they will be read much faster than if they weren’t in the cache.
_
The reason everyone should really know about things like this is because the performance difference can be absolutely dramatic. About 5 years ago, I was working on optimizing the performance of a project that had code like in the second function. I was able to increase the speed of some loops just like that by a factor of 10 when programming in C by accessing memory sequentially.