In most projects I don't see any -Ox
flags, which you think would be standard for every project, since it could dramatically increase the speed of the program.
Are there any specific reasons to not compile with -Ox
or it's non-gcc counterparts?
It's much easier to debug an unoptimised program, because the object code tends to be a more direct translation of the source code. With optimisation enabled the compiler might re-order statements or eliminate them entirely by coalescing several operations into one. That means when debugging a program (or a core dump) there isn't such a direct mapping from a position in the program image to a line of source code.
GCC 4.8 adds a new optimisation level that is a great compromise between performance and debuggability:
A new general optimization level,
-Og
, has been introduced. It addresses the need for fast compilation and a superior debugging experience while providing a reasonable level of runtime performance. Overall experience for development should be better than the default optimization level-O0
.
With -Og
the compiler does simple optimisations that don't make it harder to debug and don't take too long to compile, so the code performs better than completely unoptimised code but can still be debugged.
One reason is if you want to step through the code with a debugger. If optimizations are enabled statements can be reordered or missed out and the execution of the program will not have to follow what is in the source code step-by-step. Values of variables may not be available because they have been optimized out. Thus it is hard to reason about what the program is doing as you run it in the debugger.
Another reason for avoiding optimization is that you have used undefined behaviour in your program, and optimization could cause your program to break. (In fact, that is a reason for using optimization - to find such bugs.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With