难道"若([布尔] ==真)"需要比&QUOT一个步骤;如果([布尔])QUOT ;? [英] Does "if ([bool] == true)" require one more step than "if ([bool])"?

查看:132
本文介绍了难道"若([布尔] ==真)"需要比&QUOT一个步骤;如果([布尔])QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个纯粹的学究式的问题,沙爹我自己的​​好奇心。

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

我倾向于去,在这个问题后面的选项(这样:如果(boolCheck){...} ),而一个同事始终写入前者(如果(boolCheck ==真){...} )。我总有种取笑他了,他总是解释它作为当他第一次开始编程的老习惯。

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.

但它只是今天发生,我认为实际写出整个 ==真部分实际上可能需要处理一个额外的步骤,因为任何前pression以 == 运营商被评估为布尔值。这是真的吗?

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?

在换句话说,按照我的理解,选项的没有的==真行可以宽松地描述如下:

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


  1. 检查X

虽然选项的==真行会更喜欢:

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


  1. 设Y是真实的,如果X是真,否则为false

  2. 检查是

我是正确的?或者任何正常的编译器/间preTER将废除这种差异?还是我忽视的东西,而且也确实没有区别呢?

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.

推荐答案

编译器应该产生相同的code。然而,真正的比较可以说是较好的,因为它是更明确。一般来说,我没有做明确的对比,但你不应该取笑他做了。

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;
}

在这一点上,问题是做测试和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.

这篇关于难道"若([布尔] ==真)"需要比&QUOT一个步骤;如果([布尔])QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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