简体   繁体   English

C ++优化语句

[英]C++ Optimizing If Statements

Consider following statement: 请考虑以下声明:

 C a, b; //C contains c1, c2 and c3 all integers

if(a.c1==b.c1 && a.c2 == b.c2) {
     a.c3=b.c3;
 }

Will this statement be optimized to the following: 此声明是否会针对以下内容进行优化:

 if(a.c1 == b.c1) {
    if(a.c2 == b.c2) {
       a.c3=b.c3;
    }
 }

AFAIK, C++ compilers does not perform this kind of operation since it can have side effects. AFAIK,C ++编译器不执行此类操作,因为它可能有副作用。 But these are built-in types. 但这些都是内置类型。

  • Is there anything related in the standard? 标准中有什么相关内容吗?
  • If it is compiler specific is main stream compilers (MS, GNU, Intel) are doing it or not? 如果它是编译器特定的主流编译器(MS,GNU,Intel)正在做或不做?

Yes. 是。 The following code snippet: 以下代码段:

C a, b; //C contains c1, c2 and c3 all integers
if(a.c1==b.c1 && a.c2 == b.c2)
{
    a.c3=b.c3;
}

will be "optimized" to this (or something equivalent): 将被“优化”到这个(或类似的东西):

if(a.c1 == b.c1)
{
    if(a.c2 == b.c2)
    {
        a.c3=b.c3
    }
}

This is required not because of optimization but because the C++ standard requires short-circuit evaluation . 这不是因为优化,而是因为C ++标准需要 短路评估 So reasonably standards-conforming C++ compilers should be able to short-circuit that. 因此,合理的符合标准的C ++编译器应该能够使其短路。

There isn't a single place in the C++ standard that explicitly states that some boolean operators are short-circuited. C ++标准中没有一个地方明确声明某些布尔运算符是短路的。 It is implied from the rules: 这是从规则暗示:

ISO/IEC C++ Standard 14882 §5.14 Logical AND operator [expr.log.and] ISO / IEC C ++标准14882§5.14逻辑AND运算符[expr.log.and]

 logical-and-expression: inclusive-or-expression logical-and-expression && inclusive-or-expression 
  1. The && operator groups left-to-right. &&运算符从左到右分组。 The operands are both implicitly converted to type bool (clause 4). 操作数都隐式转换为bool类型(第4节)。 The result is true if both operands are true and false otherwise. 其结果是true ,如果两个操作数都是truefalse ,否则。 Unlike & , && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false . &不同, &&保证从左到右的评估:如果第一个操作数为false则不评估第二个操作数。

The rules are similar for the || ||的规则类似 operator: 运营商:

ISO/IEC C++ Standard 14882 §5.15 Logical OR operator [expr.log.or] ISO / IEC C ++标准14882§5.15逻辑OR运算符[expr.log.or]

 logical-or-expression: logical-and-expression logical-or-expression || logical-and-expression 
  1. The || || operator groups left-to-right. 操作员组从左到右。 The operands are both implicitly converted to bool (clause 4). 操作数都隐式转换为bool (第4节)。 It returns true if either of its operands is true , and false otherwise. 如果其任一操作数为true ,则返回true否则返回false Unlike | |不同 , || || guarantees left-to-right evaluation; 保证从左到右的评估; moreover, the second operand is not evaluated if the first operand evaluates to true . 此外,如果第一个操作数的计算结果为true则不计算第二个操作数。

And the conditional ? 有条件的? operator: 运营商:

ISO/IEC C++ Standard 14882 §5.16 Conditional operator [expr.cond] conditional-expression: logical-or-expression logical-or-expression ? ISO / IEC C ++标准14882§5.16条件运算符[expr.cond] conditional-expression:logical-or-expression logical-or-expression? expression : assignment-expression 表达式:赋值表达式

  1. Conditional expressions group right-to-left. 条件表达式从右到左分组。 The first expression is implicitly converted to bool (clause 4). 第一个表达式隐式转换为bool (第4节)。 It is evaluated and if it is true , the result of the conditional expression is the value of the second expression, otherwise that of the third expression. 它被评估,并且如果它是true ,条件表达式的结果是所述第二表达式的值,否则,所述第三表达的。 All side effects of the first expression except for destruction of temporaries (12.2) happen before the second or third expression is evaluated. 除了临时破坏(12.2)之外,第一个表达式的所有副作用都发生在评估第二个或第三个表达之前。 Only one of the second and third expressions is evaluated. 仅评估第二和第三表达式中的一个。

Standard 5.14 / 1 : 标准5.14 / 1:

The && operator groups left-to-right. &&运算符从左到右分组。 The operands are both implicitly converted to type bool (clause 4). 操作数都隐式转换为bool类型(第4节)。 The result is true if both operands are true and false otherwise. 如果两个操作数都为真,则结果为true,否则为false。 Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false. 与&不同,&&保证从左到右的评估: 如果第一个操作数为假,则不评估第二个操作数。

C++ uses shirt-circuit evaluation . C ++使用衬衫电路评估 So, in a way it will work that way. 因此,在某种程度上它将以这种方式工作。

I doesn't actually evaluate and optimize the code that way, but it will evaluate to the equivalent in assembly code. 我实际上并没有以这种方式评估和优化代码,但它将在汇编代码中进行评估。

The && operator is short-circuiting. &&运算符是短路的。 In other words, if a.c1 == b.c1 fails to evaluate to true, then a.c2 == b.c2 won't even be evaluated. 换句话说,如果a.c1 == b.c1无法评估为true,则甚至不会评估a.c2 == b.c2。

It is similar to the solution you describe, but it is part of the C++ language (and seems much easier to read than explicitly typing nested if statements). 它类似于您描述的解决方案,但它是C ++语言的一部分(并且比显式键入嵌套的if语句更容易阅读)。

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

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