Why is ===
faster than ==
in PHP?
问题:
回答1:
Because the equality operator ==
coerces, or converts the data type temporarily to see if it's equal to the other operand whereas ===
( identity operator ) doesn't need to do any converting whatsoever and thus less work is done, making it faster.
回答2:
===
does not perform typecasting, so 0 == '0'
evaluates to true
, but 0 === '0'
- to false
.
回答3:
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.
Therefore, === is quicker at checking a fail condition
回答4:
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 == $b
vs$a === $b
- where
$a
and$b
were random integers [1, 100] - the two variables were generated and compared one million times
- the tests were run 10 times
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.
回答5:
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.
回答6:
The == incurs a larger overhead of type conversion before comparison. === first checks the type, then proceeds without having to do any type conversion.
回答7:
Because ===
doesn't need to coerce the operands to be of the same type before comparing them.
I doubt the difference in speed is very much though. Under normal circumstances you should use whichever operator makes more sense.
回答8:
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.
回答9:
Faster should 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
回答10:
If the test results are correct, then it must be a compiler issue,
The processor will do whatever it is told to do on a clock cycle
If it has less to do then it will be quicker to do
Addition:
Ah well actually if the compiler has already created loads of machine code to be processed, then if it has already added zillions of stuff to cope with what type of data needs comparing, then the removal of one "minor" IF will not change speeds much at all.
If anyone still reads this are then I am interesting in more discussion.
Phil