Once I realized there is the option for this in GCC, I asked google and plenty of people want to know how to tell the compiler not to optimize the code. This seems counter-productive, what purpose can this serve to help the programmer? Debugging perhaps? How would it help in a situation where it is preferred to do this?
Share
The main reason is compile time: turning on optimizations can significantly increase build times without necessarily giving much benefit.
Also, certain optimizations can affect the accuracy and correctness of your program. However, these optimizations usually need to be turned on explicitly rather than with a flag like -O2.
Some optimizations–things like inlining–can increase the size of the executable. In certain cases, this is an important consideration.
Optimization can also have negative effects on your code. For example, speed might increase but at the expense of using more memory. This is not always desirable.