简体   繁体   English

是否有可视的c ++预定义预处理器宏,可让您知道编译器何时进行优化

[英]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): 我希望能够使用可视c ++编译器(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: debug_mode标志的用法如下:

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 :-) 问题是在调试实例中,我不断执行此操作会不断失败并轰炸我,因为奇怪的是,我执行代码的时间比CPU通常通过它的时间长得多:-)

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(). 目前,我正在使用两次调用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? 在回应一些解释为什么我不想做自己想做的事的评论时,我也许应该将问题改写为简单的话:cl中是否有gcc的__optimize__等效项?

You could use either _DEBUG or NDEBUG to detect the debug configuration. 您可以使用_DEBUGNDEBUG来检测调试配置。 This technically doesn't mean the same thing as the optimization flag, but 99% of the time this should suffice. 从技术上讲,这与优化标志的含义不同,但是99%的时间就足够了。

Another option would be to add a preprocessor definition to the project yourself. 另一种选择是自己向项目添加预处理器定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM