Search code examples
c++visual-studiooptimizationcompiler-optimization

Difference Between Release Mode vs Release + O2 Mode in MSVC c++


In Visual Studio, to speed up my program, there are two things that I tweak around with:

The first one is to turn Debug mode into Release mode.

The second one is to turn on -O2 (so you must turn off Basic Runtime Checks) in project properties.

Normally, I use them together, but I have heard (?) that Release mode includes -O2 optimization so I am just curious if that is true.

I tried many programs of my previous projects and it was clear that Release + O2 mode performs 3~10x faster than the original program with no optimization. But I am not clear whether only Release mode is needed to achieve a program that fast.


Solution

  • Each configuration in Visual Studio defines a set of parameters for the project.

    When you create a new project, it comes by default with 2 configuration: Debug and Release (you can add more if you need via the Configuration Manager).

    Typically in Debug the project is built with debug information, and with no optimzation, and in Release the project is built without debug information and with some optimizations.

    But these are all just defaults, and you can change them to meet your needs.

    In order to maximize performance it is recommended to start with the Release configuration.
    My MSVC 2022 sets it by default with O2 optimization.
    You can try to change various optimization settings and profile your code if you want to achive peak performance.

    The optimization settings are under Project properties -> C/C++ -> Optimization:

    enter image description here