sji
sji

Reputation: 1907

Is there a visual c++ predefined preprocessor macro that lets you know when the compiler is optimizing

I would like to be able to do something like this using visual c++ compiler (vc12):

// If we have compiled with O2
#ifdef _O2_FLAG_
bool debug_mode = false;

// If we are in dirty slow non optimized land
#else
bool debug_mode = true;
#endif

But I cannot find a predefined macro for this purpose.

Context:

The debug_mode flag is used like:

if (!debug_mode && search_timer->seconds_elapsed() > 20) {
   return best_result_so_far;
}

The problem being that in a debug instance that I step through this constantly fails and bombs me out because strangely it takes me a lot longer to step through the code than the CPU normally goes through it :-)

If there is some underlying clock that pauses when debug does that would also solve my problem. Currently I am using the difference between two calls to std::chrono::high_res_clock::now().

EDIT:

In response to several comments explaining why I don't want to do what I want to do, I should perhaps reword the question as simply: Is there an equivalent of gcc's __optimize__ in cl?

Upvotes: 3

Views: 411

Answers (1)

Rick de Water
Rick de Water

Reputation: 2632

You could use either _DEBUG or NDEBUG to detect the debug configuration. This technically doesn't mean the same thing as the optimization flag, but 99% of the time this should suffice.

Another option would be to add a preprocessor definition to the project yourself.

Upvotes: 4

Related Questions