Why is ===
faster than ==
in PHP?
===
does not perform typecasting, so 0 == '0'
evaluates to true
, but 0 === '0'
- to false
.
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.
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
$a
and $b
were random integers [1, 100]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.
In php (c code) value is a "class" like:
class value
{
$int_;
$float_;
$string_;
$array_;
$object_;
}
When your are comparing $a == $b
and $a
is int
type, there will be something like:
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
.
When you are comparing $a === $b
and $a
is int
type, there will be someting like:
if ($a->int_ == $b->int_)
I found out that there actually is a significant speed difference between the 2 operators. Results for php 8.0.0 RC5 and php 7.4.12 running in docker container below. The project is hosted on github so everyone can review the methodology. Disclaimer: I built the tool.
$ php src/benchmark.php --custom --filter ~equal~
PHP benchmark
-------------------------------
platform : Linux x64
php version : 7.4.12
xdebug : off
memory limit : 128M
max execution : 0
time per iteration : 50ms
iterations : 100
-------------------------------
---------------------------------------------------
0 : == ===
mean : 394156 459015 +16.5%
median : 397448 461822 +16.2%
mode : 398154 458062 +15.0%
minimum : 313271 405692 +29.5%
maximum : 411157 480360 +16.8%
quartile 1 : 393222 454952 +15.7%
quartile 3 : 400881 466491 +16.4%
IQ range : 7659 11538 +50.7%
std deviation : 15593 11867 -23.9%
normality : 0.8% 0.8%
---------------------------------------------------
$ php src/benchmark.php --custom --filter ~equal~
PHP benchmark
-------------------------------
platform : Linux x64
php version : 8.0.0RC5
xdebug : off
memory limit : 128M
max execution : 0
time per iteration : 50ms
iterations : 100
-------------------------------
---------------------------------------------------
0 : == ===
mean : 405032 474768 +17.2%
median : 409226 477313 +16.6%
mode : 408421 479741 +17.5%
minimum : 311606 386509 +24.0%
maximum : 417895 491842 +17.7%
quartile 1 : 405740 473436 +16.7%
quartile 3 : 412677 483139 +17.1%
IQ range : 6937 9703 +39.9%
std deviation : 17501 15657 -10.5%
normality : 1.0% 1.0%
---------------------------------------------------
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