c++

Is the running speed of a program related to Debug mode or Release mode?


The speed of my program running in Debug mode and Release mode differs much in VS2012. However, the speed remained almost unchanged when I run my program in linux using g++ no matter whether I used Debug mode (with -g option) or release mode. Can any one tell me if this is ordinary?


Solution

  • In gcc (and clang, as well as other C/C++ compilers), debug information and optimization are independent options. There is no single option for "Release mode"; you need to define more precisely what you want.

    The -g option specifies that you want debug information to be produced. You can be more precise -- it's possible to request a specific format for the debug information -- but in normal circumstances, that's not necessary. Leaving out -g does not automatically enable optimizations.

    Optimization is controlled by the -O option, and by a large number of specific optimization flags. If you don't specify any optimization flag, practically no optimizations will be performed.

    There are four basic levels of optimization:

    -O0  no optimizations that would affect debugging (the default)
    -O or -O1  basic optimizations
    -O2  more optimizations
    -O3  yet more optimizations
    

    and

    -Os  similar to -O2, but optimizing also for executable size.
    

    The specific flags start with -f. There are hundreds of them. Usually you don't need to worry about it, though.

    For some architectures, you can also optimize (or not) based on the target platform. For example, a default i386 build will not make use of all of the features of modern chips.

    If you specify an optimization option other than -O0 (which is the default if you don't specify any -O option), then the compiler may reorder statements, move variables to registers, and otherwise make life difficult for the debugger (that is, both the person doing the debugging, and the software they use to help them). gdb will do its best to cope , and gcc will provide hints for gdb to help it, but you will probably still find it confusing. However, it's common to build with -g even in optimized builds because it makes reading core images easier.