Earlier quoted context omitted.
You can't really do loop unrolling and constant folding as some suggest because the number of iterations is determined at run time. But assuming his CPU takes one cycle for an addition and one cycle for a conditional jump, his CPU only needs to run at 1GHz to achieve his result. Given that branch prediction should be nearly perfect for such a simple and short loop, modern x86 CPUs doing at least 4 integer additions p…
> You can't really do loop unrolling and constant folding as some suggest because the number of iterations is determined at run time. Huh?
>NUMBER = atoi(argv[1]);
argv is populated at runtime based on what the OS passed you.
But you are right if you meant that my statement is too strong: you can't do arbitrary loop unrolling. You can jump to a loop that's unrolled once if the number is even, etc.
There are certainly also optimizers that will just output the input without any loop, but that's beyond anything I would expect of a current production compiler.