I chose David\'s answer because he was the only one to present a solution to the difference in the for-loops with no optimization flags. The other answers demonstrate what
Checking the assembly output is really the only way to illuminate such things.
Compiler optimisations will do a great deal of things, including things that are not strictly "standard compliant" (although, that is not the case with -O1
and -O2
, to my knowledge) - for instance check, -Ofast
switch.
I have found this helpful: http://gcc.godbolt.org/, and with your demo code here