是否“如果([bool] == true)"?需要比“if ([bool])"多一步吗? [英] Does "if ([bool] == true)" require one more step than "if ([bool])"?

查看:22
本文介绍了是否“如果([bool] == true)"?需要比“if ([bool])"多一步吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个纯粹的迂腐问题,以满足我自己的好奇心.

This is a purely pedantic question, to sate my own curiosity.

我倾向于在问题中选择后一个选项(因此:if (boolCheck) { ... }),而同事总是写前一个选项(if (boolCheck == true) { ... }).我总是取笑他,他总是把它解释为他刚开始编程时的老习惯.

I tend to go with the latter option in the question (so: if (boolCheck) { ... }), while a coworker always writes the former (if (boolCheck == true) { ... }). I always kind of teased him about it, and he always explained it as an old habit from when he was first starting programming.

但我今天突然想到,实际上写出整个 == true 部分实际上可能需要额外的处理步骤,因为任何带有 == 运算符被评估为布尔值.这是真的吗?

But it just occurred to me today that actually writing out the whole == true part may in fact require an additional step for processing, since any expression with a == operator gets evaluated to a Boolean value. Is this true?

换句话说,据我了解,without == true 行的选项可以大致描述如下:

In other words, as I understand it, the option without the == true line could be loosely described as follows:

  1. 检查 X

虽然 with 选项的 == true 行更像:

While the option with the == true line would be more like:

  1. 如果 X 为真,则令 Y 为真,否则为假
  2. 勾选是

我说的对吗?或者也许任何普通的编译器/解释器都会消除这种差异?还是我忽略了什么,真的没有任何区别?

Am I correct? Or perhaps any normal compiler/interpreter will do away with this difference? Or am I overlooking something, and there's really no difference at all?

显然,在实际观察到的性能方面不会有任何差异.就像我说的,我只是好奇.

Obviously, there will be no difference in terms of actual observed performance. Like I said, I'm just curious.

感谢所有实际发布编译结果以说明两种方法之间的步骤是否不同的人.(似乎大多数时候都是这样,尽管只是轻微的.)

Thanks to everyone who actually posted compiled results to illustrate whether the steps were different between the two approaches. (It seems, most of the time, they were, albeit only slightly.)

我只想重申,我不是询问什么是正确"的方法.我知道很多人偏爱其中之一.我也明白,从逻辑上讲,两者是相同的.我只是好奇 CPU 执行的实际操作是否完全相同两种方法;事实证明,很多时候(显然这取决于语言、编译器等),它们不是.

I just want to reiterate that I was not asking about what is the "right" approach. I understand that many people favor one over the other. I also understand that, logically, the two are identical. I was just curious if the actual operations being performed by the CPU are exactly the same for both methods; as it turns out, much of the time (obviously it depends on language, compiler, etc.), they are not.

推荐答案

编译器应该生成相同的代码.但是,与 true 进行比较可以说更好,因为它更明确.一般来说,我不做明确的比较,但你不应该取笑他这样做.

The compiler should generate the same code. However, comparing with true is arguably better because it is more explicit. Generally I don't do the explicit comparison, but you shouldn't make fun of him for doing it.

最简单的判断方法是尝试.MS 编译器 (cl.exe) 在汇编中生成相同数量的步骤:

The easiest way to tell is to try. The MS compiler (cl.exe) generates the same number of steps in assembly:

int _tmain(int argc, _TCHAR* argv[])
{
    bool test_me = true;

    if (test_me) {
004113C2  movzx       eax,byte ptr [test_me] 
004113C6  test        eax,eax 
004113C8  je          wmain+41h (4113E1h) 
        printf("test_me was true!");
    }

    if (test_me == true) {
004113E1  movzx       eax,byte ptr [test_me] 
004113E5  cmp         eax,1 
004113E8  jne         wmain+61h (411401h) 
        printf("still true!");
    }
    return 0;
}

此时的问题是 test 和 cmp 是否具有相同的成本?我的猜测是肯定的,尽管专家可能能够指出差异.

At this point the question is do test and cmp have the same cost? My guess is yes, though experts may be able to point out differences.

实际结果是您不必担心这一点.您可能有更大的性能鱼可炒.

The practical upshot is you shouldn't worry about this. Chances are you have way bigger performance fish to fry.

这篇关于是否“如果([bool] == true)"?需要比“if ([bool])"多一步吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆