空如果优化
Empty If Optimization
考虑以下因素:
int status = 0;
while(status < 3)
{
switch(status)
{
case 0:
// Do something
break;
case 1:
if(cond1 && cond2 || cond3 && cond4)
; // status = 1
else if(cond5)
status = 2;
else
status = 0;
// there could be more else-if statements
break;
case 2:
// Do something else
break;
}
status++;
}
考虑到第一个 if 语句只是为了可读性而存在,并且如图所示,它的主体是空的(因为冗余),我想知道编译器如何(提前或即时编译器)可能对此进行优化(如果可能进行任何优化)。
对于通用编译器,答案是否定的。
然而,两个密切相关的优化是 Data flow optimizations, which aims to eliminate double calculations and impossible paths in code. Another is Currying,旨在针对特定参数优化通用函数。
考虑以下因素:
int status = 0;
while(status < 3)
{
switch(status)
{
case 0:
// Do something
break;
case 1:
if(cond1 && cond2 || cond3 && cond4)
; // status = 1
else if(cond5)
status = 2;
else
status = 0;
// there could be more else-if statements
break;
case 2:
// Do something else
break;
}
status++;
}
考虑到第一个 if 语句只是为了可读性而存在,并且如图所示,它的主体是空的(因为冗余),我想知道编译器如何(提前或即时编译器)可能对此进行优化(如果可能进行任何优化)。
对于通用编译器,答案是否定的。
然而,两个密切相关的优化是 Data flow optimizations, which aims to eliminate double calculations and impossible paths in code. Another is Currying,旨在针对特定参数优化通用函数。