Does it affect the performance in any way? As a matter of fact it does.
For a value of n = 4096, I got the following runtimes for different orders of nested loops. (BTW ran it on my mac, while writing this up. Should have fired up an EC2 for more accurate results.)
4096 x 4096
- i, j, k -> 1255.99 sec
- i, k, j -> 200.38 sec
- j, i, k -> 1115.81 sec
- j, k, i -> 3103.15 sec
- k, i, j -> 210.53 sec
- k, j, i -> 3042.23 sec
Apparently, the slowest we have is 3103.15 seconds and the fastest is 200.38 seconds. Here, the loop order affects running time by a factor of 15.4.
What if we increase n, does the factor increase linearly or stays constant or increases exponentially?
May be a weekend exercise for you. Give it a try, I’ll quickly go into why this happened.