嵌套if语句中的代码

问题描述:

是否对嵌套ifs中运行的“代码行”有任何性能影响?嵌套if语句中的代码

if (condition_1) 
{ 
    if (condition_2) 
    { 
     if (condition_n) 
     { 
      /* Lines of code */ 
     } 
    } 
} 

不,不应该有性能影响。任何体面的编译器都应该能够轻松应对并正确地对其进行优化。您的代码最大的问题不在于性能,而在于可读性。

顺便说一句,你可以轻松地将它改写为以下更可读的代码:

if (condition_1 && 
    condition_2 && 
    ...etc...) 
{ 
    /* Lines of code */ 
} 
+0

除非有condition_2`或`condition_n`的`块之后的代码,在这种情况下,你的代码是不等价到OP的代码。 – wilhelmtell 2011-01-28 00:59:16