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.
It is compiler specific, but I would imagine that in nearly all cases the performance will be the same for both approaches.
To be sure for a specific situation you should measure the performance, although before you spend many hours micro-optimizing code like this you should first be sure that this is in fact the bottleneck in your application (probably it isn't).
Your second example could be writen as a for
loop with no initialization expression.
int age = 17; // This was made for something else in the code
for (; age < 25; age++) {
cout << age << endl;
}
It won't make any difference with compiler optimization!
My 2cts, when it deals about loop implementation choice, it's important to use the one which suits the algorithm logic. It's easier to read the code.
Your question as you give it is ill posed. The IO that you have inside the loops dominates all that the loop handling statements ever could add by at least one order of magnitude. My bet would even be that you can't measure any significant difference since it would not be distinguishable from the noise (in terms of measurement) that the IO produces.
The thing would only matter, if the code inside the loop would be really fast. Here fast meaning not even to have a memory read instruction, since then already CPU memory bandwidth would dominate, again.
So if you are really curious, and you seem to be, read the assembler and look at the code. If you only have two small functions with just each type of loop, the output is not so difficult to read, really, don't be scared. I'd suggest to do it with C, though, this is probably closest to the assembly and easiest for you to identify the parts.
You didn't tell us on what system/compiler you are. With gcc the options would be -S -O3 -march=native
to have it produce a .s
file.
All sensible compilers should compile equivalent loops to identical assembly / IL code involving branches and jumps. (at least with optimizations enabled)
On certain CPU architectures, the characteristics of the loop may provide opportunities for more optimization than the choice of for
versus while
.
In particular, FORTRAN
and Pascal
recommended a constant (rather than a variable) for the number of loop iterations which some CPUs can optimize. For example, on a Cyber (a 1970s Iron Dinosaur mainframe), a 15-bit register holds the for
loop index which can easily be compared. A while
instead uses one or two of the harder-to-access 60-bit registers. Also, a Cyber branch instruction is considerably more expensive than the loop housekeeping, or possibly the loop content. In such cases, a high level of optimization might unroll the loop to avoid all the overhead.
However, modern code generators don't work like they used to: source code is turned into an intermediate parse structure which abstracts such choices away. In short, for
versus while
makes no difference.