[英]Multiple If statements versus single statement with conditions evaluated using logical operators
I am writing a function that checks several conditions before actually executing its task.我正在编写一个函数,在实际执行其任务之前检查几个条件。 This is done by a number of
if
statements.这是由许多
if
语句完成的。 Like so:像这样:
bool foo()
{
if(invalid())
return false;
if(dont_execute())
return false;
// .. etc
// Actual execution here
return true;
}
In this function is there any benefits by changing the multiple conditions to :在此函数中,将多个条件更改为:
bool foo()
{
if(invalid() || dont_execute() /* || .. etc */)
return false;
// Actual execution here
return true;
}
I feel that the first style is more readable.我觉得第一种风格更具可读性。 What I want to know is, if there is any performance impact in using multiple if statements rather than combining using logical operators.
我想知道的是,使用多个 if 语句而不是组合使用逻辑运算符是否对性能有任何影响。
No there is no performance impact.不,没有性能影响。 If we compare the assembly of both functions we can see that it is identical for both functions.
如果我们比较两个函数的汇编,我们可以看到这两个函数是相同的。
Example:例子:
bool f1();
bool f2();
bool combined()
{
if (f1() || f2())
return false;
return true;
}
bool separate()
{
if (f1())
return false;
if (f2())
return false;
return true;
}
And here the assembly:这里是大会:
combined():
sub rsp, 8
call f1()
mov r8d, eax
xor eax, eax
test r8b, r8b
jne .L1
call f2()
xor eax, 1
.L1:
add rsp, 8
ret
separate():
sub rsp, 8
call f1()
mov r8d, eax
xor eax, eax
test r8b, r8b
jne .L7
call f2()
xor eax, 1
.L7:
add rsp, 8
ret
Using the test case使用测试用例
bool invalid();
bool dont_execute();
void execute();
bool foo()
{
if(invalid())
return false;
if(dont_execute())
return false;
execute();
return true;
}
bool foo2()
{
if(invalid() || dont_execute() /* || .. etc */)
return false;
execute();
return true;
}
you can see that both foo
and foo2
are compiled to the exact same assembly by both GCC 9.2 and Clang 9 with -O2
optimization flags, see godbolt .您可以看到
foo
和foo2
都被 GCC 9.2 和 Clang 9 编译为完全相同的程序集,并带有-O2
优化标志,请参阅Godbolt 。 For example GCC's output is例如 GCC 的输出是
foo():
sub rsp, 8
call invalid()
test al, al
je .L2
.L4:
xor eax, eax
add rsp, 8
ret
.L2:
call dont_execute()
test al, al
jne .L4
call execute()
mov eax, 1
add rsp, 8
ret
foo2():
sub rsp, 8
call invalid()
test al, al
je .L8
.L10:
xor eax, eax
add rsp, 8
ret
.L8:
call dont_execute()
test al, al
jne .L10
call execute()
mov eax, 1
add rsp, 8
ret
While this does not mean that there will never be a difference, at least the compilers don't think they need to do anything different in the two cases, even if they don't know what the functions called do.虽然这并不意味着永远不会有区别,但至少编译器认为他们不需要在这两种情况下做任何不同的事情,即使他们不知道被调用的函数做了什么。
So I suggest you don't worry about performance, but go with what you consider more readable.所以我建议你不要担心性能,而是选择你认为更具可读性的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.