Jon*_*ter 3 .net c# performance
假设我在c#中有以下代码片段
static const bool DO_PERFORMANCE_CODE = false;
if (DO_PERFORMANCE_CODE)
{
// performance monitoring code goes here
}
Run Code Online (Sandbox Code Playgroud)
编译器会删除该代码吗?这是我想要的功能.基本上我想模仿C#中的条件编译,但我想要除Release和Debug之外的更多配置.如果有更好的方法,我会乐于听到它.
在"debug"中构建时,定义了预处理器变量DEBUG.所以,你可以这样做:
public void MyFunc()
{
//do release stuff
#if DEBUG
//do performance testing
#endif
//finish release stuff
}
Run Code Online (Sandbox Code Playgroud)
当切换到Release模式时,编译器会忽略它.
或者,如果您不想在调试模式下进行测试,可以定义自己的预处理器变量,确保在要测试时"#define PERFORMANCE_TESTING",并在不需要时将其注释掉.
#define PERFORMANCE_TESTING
public void MyFunc()
{
//do release stuff
#if PERFORMANCE_TESTING
//do performance testing
#endif
//finish release stuff
}
Run Code Online (Sandbox Code Playgroud)