Reputation: 774
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?
Upvotes: 4
Views: 164
Reputation: 6006
On a somewhat older project, all our tests were done with debug build, including a lot of print statements. For the final build, delivered to the customer, it was decided not to use a less-tested retail build (using gcc's full optimization options), since that could introduce timing related issues (actually, the finding of defects now masked due to the specific timing of the debug builds), and since the customer was happy enough with the current speed of operation.
On my current project, a lot of code is to be placed in ROM (initially: all), and then we obviously don't want to remove dead code, as future updates - to be placed in ram - can then still make use of the rom code, reducing space requirements in ram.
Also, what would be the default? Optimize for space, or for execution time? Not choosing is the only right choice to make.
Upvotes: 1
Reputation: 171263
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.
Upvotes: 4
Reputation: 3154
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.)
Upvotes: 3
Reputation: 20366
This makes Debug more accurate without optimization.
Unused variables, redundant statements will not be skipped.
Upvotes: 1