2

Possible Duplicate:
Why does the order of the loops affect performance when iterating over a 2D array?

I have this simple for loops

for (i=0;i<10000;i++){
   for(j=0;j<10000;j++){
      a[i][j]=i+j;
      }}

when I change the order of these for loops to:

for (j=0;j<10000;j++){
   for(i=0;i<10000;i++){
      a[i][j]=i+j;
      }}

I see that the runtime increases dramatically. Why this happen?

thanks

Community
  • 1
  • 1
peaceman
  • 1,499
  • 2
  • 17
  • 29

1 Answers1

1

The loss of locality of access causes more page faults.

James Kanze
  • 150,581
  • 18
  • 184
  • 329