【问题标题】:Possible to hide #define / Macros in Visual Studio 2010?可以在 Visual Studio 2010 中隐藏 #define / 宏吗?
【发布时间】:2014-01-24 22:17:11
【问题描述】:

我有一些使用秒表检测的代码,以便记录执行时间的去向。我不想总是运行它,所以我认为处理计时器的最有效方法是使用预处理器宏。对于下面的例子,如果定义了ENABLE_ENGR_PROFILING,我在秒表上调用start()函数,做一些工作,然后在秒表上调用accumulate(),它将从start()测量的时间增量添加到时间计数器。然后在程序结束时,我可以转储时间,看看时间都去哪儿了。这很好用,但它增加了很多代码。当我在研究算法时,我不想被计时器代码分心。在下面的 7 行中,我真的只想在编写程序时看到 1 行,someTimeConsumingCode()。有没有办法在 Visual Studio 2010 中隐藏这些宏?

#if defined(ENABLE_ENGR_PROFILING)
stopwatch.start();  
#endif
someTimeConsumingCode();
#if defined(ENABLE_ENGR_PROFILING)
stopwatch.accumulate(); 
#endif

【问题讨论】:

  • “研究算法”是什么意思?
  • 算法,我指的是除了定时器之外的所有东西......在处理算法/主程序代码时,我想忽略定时器(看不到它们)。
  • 你能提前到 2012 年或 2013 年吗? Lambda 使这种东西比宏更好......
  • @486DX2-66 - 那么分心是如何发生的呢?
  • 您可以使用 Visual Studio 大纲功能折叠 if 块。

标签: c++ visual-studio-2010 macros c-preprocessor


【解决方案1】:

我不认为你可以完全隐藏代码,但你可以定义一些宏来清理混乱:

#if defined(ENABLE_ENGR_PROFILING)
#define DECLARE_STOPWATCH() TStopWatch stopwatch; // <- whatever your class type actually is
#define START_STOPWATCH() stopwatch.start()
#define ACCUMULATE_STOPWATCH() stopwatch.accumulate()
#else
#define DECLARE_STOPWATCH()
#define START_STOPWATCH()
#define ACCUMULATE_STOPWATCH()
#endif

DECLARE_STOPWATCH();
...
START_STOPWATCH();
someTimeConsumingCode();
ACCUMULATE_STOPWATCH();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多