Which is better for performance? This may not be consistent with other programming languages, so if they are different, or if you can answer my question with your knowledge in a
The example you posted actually has different behavior for while
and for
.
This:
for (int x = 0; x < 100; ++x) {
foo y;
y.bar(x);
}
Is equivalent to this:
{ // extra blocks needed
int x = 0;
while (x < 100) {
{
foo y;
y.bar(x);
}
++x;
}
}
And you can expect the performance to be identical. Without the extra braces the meaning is different, and so the assembly generated may be different.
While the differences between the two is nonexistent on a modern compiler, for
may optimize better as states its behavior more explicitly.