简体   繁体   English

&&和&的优化行为

[英]Optimization behavior with && and &

Consider 考虑

 bool Fun1()
{
 ...
}
bool Fun2()
{
}

And somewhere in the main code I can have 在主代码中的某处我可以拥有

if(Fun1() && Fun2()) //option-1
{
}

or 要么

if(Fun1() & Fun2()) //option-2 
{
}

It seems with VS2012, with option-1 it is not ensured that both functions are always executed while that happens on option-2 . 似乎在VS2012中,使用option-1并不能确保总是在option-2上执行两个函数。 Specifically if one of the function returns false then other function is not executed with option-1 . 具体来说,如果函数之一返回false则其他函数不会使用option-1执行。 However the overall code requires both of them evaluated for proper behavior, which I am able to achieve with option-2 . 但是,整个代码都需要对它们两者进行评估以确保其行为正常,而我可以使用option-2来实现。 I would like to know if this behavior of option-2 can change from compiler to compiler (also with optimization levels) or ensured by the standard? 我想知道option-2这种行为是否可以在编译器之间进行更改(也可以在优化级别上进行更改),或者可以由标准来确保?

Standard explicitly says that for built-in types like bool operators && and || Standard明确表示,对于内置类型(如bool运算符&&|| are short-circuited - ie, the first argument is evaluated first, and than second argument is evaluated only if first argument doesn't give a definitive answer to the statement - ie it is false for || 是短路的-即,第一个参数首先被求值,然后第二个参数仅在第一个参数未给出该语句的确定答案时才求值-即||false , or true for && . ,对于&&则为true

Bit-wise & is following normal function call rules, so both arguments are evaluated, however, in the unspecified order. 按位&遵循正常的函数调用规则,因此两个参数均按未指定的顺序求值。

Bitwise- & will always evaluate both operands indeed. 按位&将始终确实对两个操作数求值。 The compiler cannot optimize it away due to the as-if rule. 由于使用了as-if规则,编译器无法对其进行优化。 But it is a code smell to program like that. 但这是一种代码味道 Think about people who will maintain your code. 考虑一下将维护您的代码的人。 Why is there one & instead of two? 为什么有一个&而不是两个? Is it a typo? 是错字吗? etc. 等等

I would at least pull the evaluation out of the if-statement. 至少将评估从if语句中剔除。

  bool fun12 = Fun1() & Fun2();
  if (fun12) {
    . . .

In addition, you can't extend this approach to non- bool functions. 另外,您不能将此方法扩展到非bool函数。 You'll have to compare against 0 first. 您必须先与0比较。

And finally, && evaluates the first operand and then (optionally) the second. 最后, &&评估第一个操作数, 然后 (可选)评估第二个操作数。 The order of evaluation in & is unspecified (ie random). &的评估顺序未指定(即随机)。

&& has short circuit behavior, in other words if the first operand is false, the second is not evaluated. &&具有短路行为,换句话说,如果第一个操作数为假,则不评估第二个操作数。 & doesn't have this behavior, so the second always is evaluated for sure. &没有这种行为,因此始终对第二个进行评估。 However, if you are handling booleans, always use && not &, because & is used for bitwise operations. 但是,如果要处理布尔值,请始终使用&&而不是&,因为&用于按位运算。 For your problem, use this code: 对于您的问题,请使用以下代码:

bool a = Fun1();
if(Fun2() && a){
    //Fun2 must be the left operand
}

If you want to run both the function the you can go with option-2 but It's not necessary to call both function you can go with option-1. 如果要同时运行这两个功能,则可以使用option-2,但不必同时调用这两个功能,可以同时使用option-1。

Behavior The behavior of option-1 and option-2 is not compiler dependent. 行为选项1和选项2的行为与编译器无关。

Performance The performance of option-1 is better than option-2 性能 Option-1的性能优于Option-2

This is not a matter of optimization but a matter of semantics. 这不是优化问题,而是语义问题。

&& follows the short-circuit evaluation rule and mustn't perform the second call when the first returns false. &&遵循短路评估规则,并且当第一个返回false时, 不得执行第二个调用。

& follows ordinary evaluation and must perform both calls (unless it can prove that there is no side effect). &遵循常规评估,并且必须执行两个调用(除非可以证明没有副作用)。


Anyway, one could think of a situation where for an && expression a compiler would enforce the evaluation of the two arguments in all cases (in the absence of a side effect), to exploit parallelism. 无论如何,人们可能会想到一种情况,对于&&表达式,编译器将在所有情况下(没有副作用)强制执行对两个参数的求值,以利用并行性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM