VS2008 会 c++编译器优化以下 if 语句?

发布于 2024-08-15 04:19:58 字数 671 浏览 4 评论 0原文

if (false == x) { ...}

相对于:

if (!x) { ... }

if (false == f1()) { ...}

对于:

if (!f1()) { ... }

我认为 if(false == ... 版本更具可读性。您同意吗,或者您可以提出另一个技巧吗?它会一样快吗?谢谢。

这就是为什么我不喜欢!x:

if (25 == a->function1(12345, 6789) &&
    45 == b->function1(12345, 6789) &&
    !c->someOtherFunction(123)) { ... }

以下似乎更好:

if (25 == a->function1(12345, 6789) &&
    45 == b->function1(12345, 6789) &&
    false == c->someOtherFunction(123)) { ... }
if (false == x) { ...}

as opposed to:

if (!x) { ... }

and

if (false == f1()) { ...}

as opposed to:

if (!f1()) { ... }

I think the if(false == ... version is more readable. Do you agree, or have another trick you can propose? Will it be just as fast? Thanks.

This is why I do not like !x:

if (25 == a->function1(12345, 6789) &&
    45 == b->function1(12345, 6789) &&
    !c->someOtherFunction(123)) { ... }

The following seems better:

if (25 == a->function1(12345, 6789) &&
    45 == b->function1(12345, 6789) &&
    false == c->someOtherFunction(123)) { ... }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

作死小能手 2024-08-22 04:19:58

我个人认为 if (!x) { ... } 的形式最具可读性,但现在它们都会生成相同的代码。

编辑:在您的示例中,我会执行以下操作:

if (  a->function1(12345, 6789) == 25 &&
      b->function1(12345, 6789) == 45 &&
    !(c->someOtherFunction(123))) { ... }

但这实际上只是个人喜好的事情。做对你来说最易读的事情。这不会有任何区别。

I personally find the form if (!x) { ... } most readable, but nowadays they're all going to result in the same code.

EDIT: In your example, I'd do the following:

if (  a->function1(12345, 6789) == 25 &&
      b->function1(12345, 6789) == 45 &&
    !(c->someOtherFunction(123))) { ... }

But it's really just a personal preference thing. Do what's most readable to you. It's not going to make any difference.

つ低調成傷 2024-08-22 04:19:58

我认为 if(false == ... 版本更具可读性。您同意吗,或者您可以提出其他技巧吗?

您做错了。

false == x 返回一个 bool,显然必须与 true 进行比较!

if (true == (false == x)) { ...} 会更好。 ,所以为了安全起见,并让你的代码更具可读性,我们最好这样做:

if (true == (true == (false == x))) { ...}< /code> 等等。一旦你开始比较布尔值,你会在哪里停止?为了保持一致,你必须将其与布尔值进行比较

。您正在使用的语言,并使用 if (!x) { ...},它准确地表达了您想要的内容。

  • if (false = = x) { ...} 翻译为“如果 false 等于 x”,
  • if (!x) { ...} 翻译为“如果 x 是”。不正确”。

你能诚实地、认真地说第一种形式“更具可读性”吗?你会用一句话来这么说吗? “如果 true 则 false 等于 x”?

它并没有更具可读性,它只是向任何阅读你的代码的程序员大喊“我不明白 if 语句或布尔值”。

I think the if(false == ... version is more readable. Do you agree, or have another trick you can propose?

You're doing it wrong.

false == x returns a bool, which obviously has to be compared to true!

if (true == (false == x)) { ...} would be better. But that again returns a bool, so just to be on the safe side, and make your code more readable, we'd better do this:

if (true == (true == (false == x))) { ...}. And so on. Once you start comparing booleans to booleans, where do you stop? The result will always be another boolean, and to be consistent, you have to compare that to a boolean.

Or you could learn to understand the language you're working in, and use if (!x) { ...}, which expresses exactly what you wanted.

Which do you really think is more readable?

  • if (false == x) { ...} translates to "If it is true that false is equal to x".
  • if (!x) { ...} translates to "if x is not true".

Can you honestly, seriously, really say that the first form is "more readable"? Would you ever say that in a sentence? "If it is true that false is equal to x"?

It is not more readable, it just shouts to any programmer reading your code that "I don't understand if statements or boolean values".

慢慢从新开始 2024-08-22 04:19:58

C++ 在左侧保留了以下关键字来替代右侧的运算符:

and  and_eq  bitand    &&  &=  &
or   or_eq   bitor     ||  |=  |
     xor_eq  xor           ^=  ^
not  not_eq  compl     !   !=  ~

如果您担心 ! 会丢失,那么 not 会更加突出。

if (    a->function1(12345, 6789) == 25 and
        b->function1(12345, 6789) == 45 and
        not c->someOtherFunction(123)) {
    ...
}

C++ reserves the following keywords on the left as alternatives to the operators on the right:

and  and_eq  bitand    &&  &=  &
or   or_eq   bitor     ||  |=  |
     xor_eq  xor           ^=  ^
not  not_eq  compl     !   !=  ~

If you're worried about the ! getting lost, not stands out more.

if (    a->function1(12345, 6789) == 25 and
        b->function1(12345, 6789) == 45 and
        not c->someOtherFunction(123)) {
    ...
}
分分钟 2024-08-22 04:19:58

一个好的编译器应该为两个代码块生成相同的代码。

,您应该更担心此示例中的短路评估,而不是担心 false == f1()!f1()

if (25 == a->function1(12345, 6789) &&
    45 == b->function1(12345, 6789) &&
    !c->someOtherFunction(123)) { ... }

但是 编译器将生成跳过执行b->function1()c->someOtherFunction()的代码,如果a->function1( ) 调用的计算结果恰好不同于 25 - 原因是,编译器已经知道此时整个 if () 语句的结果,因此它可以跳转到右侧地方。

如果您的代码依赖于任何被跳过的函数修改的状态,您可能会得到令人讨厌的意外。

A good compiler should generate the same code for both code blocks.

However, instead of worrying about false == f1() vs. !f1(), you should be way more worried about the short-circuit evaluation in this example:

if (25 == a->function1(12345, 6789) &&
    45 == b->function1(12345, 6789) &&
    !c->someOtherFunction(123)) { ... }

Certain compilers will generate code that will skip the execution of b->function1() and c->someOtherFunction(), if a->function1() call happens to evaluate to something different than 25 - the reason being, the compiler already knows the outcome of the whole if () statement at that point, so it can jump at the right place.

If your code depends on a state being modified by any of the skipped functions, you might get nasty surprises.

明媚殇 2024-08-22 04:19:58

这是过早优化的情况(什么时候优化过早?)。但编译器会生成相同的代码(MSVC 2008 调试模式):

if (!bVal)
    bVal = true;

if (bVal == false)
    bVal = true;

//translates to

; 70   :         if (!bVal)

cmp BYTE PTR $T5793[ebp], 0
jne SHORT $LN9@wmain
push    OFFSET $LN10@wmain
call    __RTC_UninitUse
add esp, 4
$LN9@wmain:
movzx   eax, BYTE PTR _bVal$[ebp]
test    eax, eax
jne SHORT $LN2@wmain

; 71   :             bVal = true;

mov BYTE PTR $T5793[ebp], 1
mov BYTE PTR _bVal$[ebp], 1
$LN2@wmain:

; 72   :         
; 73   :         if (bVal == false)

cmp BYTE PTR $T5793[ebp], 0
jne SHORT $LN11@wmain
push    OFFSET $LN10@wmain
call    __RTC_UninitUse
add esp, 4
$LN11@wmain:
movzx   eax, BYTE PTR _bVal$[ebp]
test    eax, eax
jne SHORT $LN1@wmain

; 74   :             bVal = true;

mov BYTE PTR $T5793[ebp], 1
mov BYTE PTR _bVal$[ebp], 1

This is a case of premature optimization (When is optimisation premature?). But the compiler will generate the same code (MSVC 2008 Debug mode):

if (!bVal)
    bVal = true;

if (bVal == false)
    bVal = true;

//translates to

; 70   :         if (!bVal)

cmp BYTE PTR $T5793[ebp], 0
jne SHORT $LN9@wmain
push    OFFSET $LN10@wmain
call    __RTC_UninitUse
add esp, 4
$LN9@wmain:
movzx   eax, BYTE PTR _bVal$[ebp]
test    eax, eax
jne SHORT $LN2@wmain

; 71   :             bVal = true;

mov BYTE PTR $T5793[ebp], 1
mov BYTE PTR _bVal$[ebp], 1
$LN2@wmain:

; 72   :         
; 73   :         if (bVal == false)

cmp BYTE PTR $T5793[ebp], 0
jne SHORT $LN11@wmain
push    OFFSET $LN10@wmain
call    __RTC_UninitUse
add esp, 4
$LN11@wmain:
movzx   eax, BYTE PTR _bVal$[ebp]
test    eax, eax
jne SHORT $LN1@wmain

; 74   :             bVal = true;

mov BYTE PTR $T5793[ebp], 1
mov BYTE PTR _bVal$[ebp], 1
浮生面具三千个 2024-08-22 04:19:58

您是否已分析并发现评估该状况是一个热点?如果没有,则执行您的编码标准要求的任何操作。

无论如何,两者都应该编译为相同的代码。您可以通过检查编译器针对这两种情况生成的汇编代码进行检查。

Have you profiled and found evaluating the condition to be a hot spot? If not, then do whatever your coding standard requires.

Both should compile to the same code anyway. You can check by inspecting the assembly code generated by the compiler for both cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文