为什么在 PHP 中 === 比 == 快?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2401478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is === faster than == in PHP?
提问by coderex
Why is ===faster than ==in PHP?
为什么===比==PHP快?
回答by meder omuraliev
Because the equality operator ==coerces, or converts, the data?type temporarily to see if it's equal to the other operand, whereas ===(the identity operator) doesn't need to do any converting whatsoever and thus less work is done, which makes it?faster.
因为相等运算符会==暂时强制或转换数据类型以查看它是否等于另一个操作数,而===(身份运算符)不需要进行任何转换,因此完成的工作较少,这使得它?快点。
回答by raveren
===does not perform typecasting, so 0 == '0'evaluates to true, but 0 === '0'- to false.
===不执行类型转换,因此0 == '0'计算为true,但是0 === '0'- false。
回答by Salman A
There are two things to consider:
有两件事需要考虑:
If operand types are different then
==and===produce different results. In that case the speed of the operators does not matter; what matters is which one produces the desired result.If operand types are same then you can use either
==or===as both will produce same results. In that case the speed of both operators is almost identical. This is because no type conversion is performed by either operators.
如果操作数类型是不同的,那么
==并===产生不同的结果。在这种情况下,操作员的速度无关紧要;重要的是哪个产生了预期的结果。如果操作数类型相同,那么您可以使用其中一个
==或===因为两者都会产生相同的结果。在这种情况下,两个操作员的速度几乎相同。这是因为任一运算符均不执行类型转换。
I compared the speed of:
我比较了以下速度:
$a == $bvs$a === $b- where
$aand$bwere random integers [1, 100] - the two variables were generated and compared one million times
- the tests were run 10 times
$a == $b对比$a === $b- 其中
$a和$b是随机整数 [1, 100] - 这两个变量被生成并比较了一百万次
- 测试运行了 10 次
And here are the results:
结果如下:
$a == $b $a === $b
--------- ---------
0.765770 0.762020
0.753041 0.825965
0.770631 0.783696
0.787824 0.781129
0.757506 0.796142
0.773537 0.796734
0.768171 0.767894
0.747850 0.777244
0.836462 0.826406
0.759361 0.773971
--------- ---------
0.772015 0.789120
You can see that the speed is almost identical.
您可以看到速度几乎相同。
回答by iblamefish
First, === checks to see if the two arguments are the same type- so the number 1 and the string '1' fails on the type check before any comparisons are actually carried out. On the other hand, == doesn't check the type first and goes ahead and converts both arguments to the same type and then does the comparison.
首先, === 检查两个参数是否是相同的类型- 因此在实际执行任何比较之前,数字 1 和字符串 '1' 在类型检查中失败。另一方面, == 不会首先检查类型,而是继续将两个参数转换为相同的类型,然后进行比较。
Therefore, === is quicker at checking a fail condition
因此, === 在检查失败条件时更快
回答by Chris
I don't really know if it's significantly faster, but === in most languages is a direct type comparison, while == will try to do type coercion if necessary/possible to gain a match.
我真的不知道它是否明显更快,但在大多数语言中 === 是直接类型比较,而 == 将在必要/可能时尝试进行类型强制以获得匹配。
回答by John Carter
Because ===doesn't need to coerce the operands to be of the same typebefore comparing them.
因为在比较它们之前===不需要强制操作数为相同类型。
I doubt the difference in speed is very much though. Under normal circumstances you should use whichever operator makes more sense.
我怀疑速度的差异非常大。在正常情况下,您应该使用更有意义的运算符。
回答by Martin
The == incurs a larger overhead of type conversion before comparison. === first checks the type, then proceeds without having to do any type conversion.
== 在比较之前会产生更大的类型转换开销。=== 首先检查类型,然后无需进行任何类型转换即可继续。
回答by D.Martin
In conclusion === is faster because don't converts the data type to see if two variables have same value, but when you need to see if two variables have same value you will use == if doesen't mather what type are variables, or === if is important also the type of variables.
总之 === 更快,因为不转换数据类型以查看两个变量是否具有相同的值,但是当您需要查看两个变量是否具有相同的值时,您将使用 == 如果不知道变量的类型是什么, or === if 变量的类型也很重要。
回答by Anthony Rutledge
Fastershould not just be measured in direct execution time (direct performance tests are almost negligible in this case). That said, I would need to see a test involving iteration, or recursion, to really see if there is a significant, cumulative difference (when used in a realistic context). The testing and debugging time you will save when dealing with edge cases should be meaningful to you, also
更快不应该只用直接执行时间来衡量(在这种情况下,直接性能测试几乎可以忽略不计)。也就是说,我需要查看涉及迭代或递归的测试,以真正查看是否存在显着的累积差异(在实际上下文中使用时)。您在处理边缘情况时节省的测试和调试时间对您来说应该是有意义的,而且
回答by d0niek
In php (c code) value is a "class" like:
在 php(c 代码)中,值是一个“类”,如:
class value
{
$int_;
$float_;
$string_;
$array_;
$object_;
}
When your are comparing $a == $band $ais inttype, there will be something like:
当您进行比较$a == $b并且$a是int类型时,将会有类似的内容:
if ($a->int_ == $b->int_ || $a->int_ == (int) $b->float_ || $a->int_ == (int) $b->string_ || ...)
but string'1'will not be cast to ascii code 49, it will be 1.
但string'1'不会被强制转换为 ascii 代码49,它将是1.
When you are comparing $a === $band $ais inttype, there will be someting like:
当您进行比较$a === $b并且$a是int类型时,会出现以下内容:
if ($a->int_ == $b->int_)

