I just read this post, and wonder if we can draw the conclusion that a big loop within a small loop must always run faster than a small loop within a big one, no matter what the code does inside the nested loop? Take an example.
int m, n;
m = 1000000;
n = 10;
Snippet A
for (int i = 0; i < n; i++)
for (int j=0; j < m; j++)
{
DoSomething();
}
Snippet B
for (int j = 0; j < m; j++)
for (int i=0; i < n; i++)
{
DoSomething();
}
Can we say that, no matter what DoSomething() actually does, snippet A always runs faster thant snippet B?
As pointed out by @stackmate, I want to expand this question into two
When the code inside nested loop is DoSomething() which means DoSomething() has nothing to do with variable i and j. What is the performance difference?
When the code inside nested loop is DoSomething(i, j) which means DoSomething(i, j) has relateship with variable i and j. What is the performance difference?
DoSomething
doesAlmostNothing
there is so little difference that there are almost certainly bigger issues hiding elsewhere in the code, grinning, while you spend your brain on this. – Lidda