VS2008 会 c++编译器优化以下 if 语句?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我个人认为
if (!x) { ... }
的形式最具可读性,但现在它们都会生成相同的代码。编辑:在您的示例中,我会执行以下操作:
但这实际上只是个人喜好的事情。做对你来说最易读的事情。这不会有任何区别。
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:
But it's really just a personal preference thing. Do what's most readable to you. It's not going to make any difference.
您做错了。
false == x
返回一个 bool,显然必须与true
进行比较!if (true == (false == x)) { ...}
会更好。 ,所以为了安全起见,并让你的代码更具可读性,我们最好这样做:if (true == (true == (false == x))) { ...}< /code> 等等。一旦你开始比较布尔值,你会在哪里停止?为了保持一致,你必须将其与布尔值进行比较
。您正在使用的语言,并使用
if (!x) { ...}
,它准确地表达了您想要的内容。if (!x) { ...}
翻译为“如果 x 是”。不正确”。你能诚实地、认真地说第一种形式“更具可读性”吗?你会用一句话来这么说吗? “如果 true 则 false 等于 x”?
它并没有更具可读性,它只是向任何阅读你的代码的程序员大喊“我不明白 if 语句或布尔值”。
You're doing it wrong.
false == x
returns a bool, which obviously has to be compared totrue
!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".
C++ 在左侧保留了以下关键字来替代右侧的运算符:
如果您担心
!
会丢失,那么not
会更加突出。C++ reserves the following keywords on the left as alternatives to the operators on the right:
If you're worried about the
!
getting lost,not
stands out more.一个好的编译器应该为两个代码块生成相同的代码。
,您应该更担心此示例中的短路评估,而不是担心
false == f1()
与!f1()
:但是 编译器将生成跳过执行
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:Certain compilers will generate code that will skip the execution of
b->function1()
andc->someOtherFunction()
, ifa->function1()
call happens to evaluate to something different than 25 - the reason being, the compiler already knows the outcome of the wholeif ()
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.
这是过早优化的情况(什么时候优化过早?)。但编译器会生成相同的代码(MSVC 2008 调试模式):
This is a case of premature optimization (When is optimisation premature?). But the compiler will generate the same code (MSVC 2008 Debug mode):
您是否已分析并发现评估该状况是一个热点?如果没有,则执行您的编码标准要求的任何操作。
无论如何,两者都应该编译为相同的代码。您可以通过检查编译器针对这两种情况生成的汇编代码进行检查。
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.