@JLBorges You might be right. I think it boils down to what he means by "calculated" I was speaking strictly of the local variable that is created when the loop is called.
In general the second version would be faster, though it depends on the implementation of your function. If it's efficient enough, you might as well not even notice the difference.
As for the reason. In your first version you obviously call the function before each iteration to check whether the condition holds. That means you compute every instruction that is within that function before every iteration.
In the second version you run that function only once and save its value, so all that's left is to compare i to that value before each iteration.
For unspecified "things" being done, one can't even know if:
1 2 3 4 5
int to = match.pathKey.count();
for ( int i=0; i<to; ++i )
{
// doSomething();
}
and
1 2 3 4
for (int i=0; i<match.pathKey.count(); ++i )
{
// doSomething();
}
are equivalent. Performance speculations for non-specific situations seems pointless. Where the latter behavior is equivalent to the former, the compiler is allowed to generate identical code for both.
skorefish wrote:
Is there a way to see what the compiler is really doing?
Generate and review an assembly listing. Make sure you're compiling with optimizations on.