I am aware that XOR is the same as not equal, but I want to ask for the reason for the performance difference here. I know it is negligible, but I am curious to know why that happens.
Analysis
My own tests:
$ php -r '$a=true; $b=true;
$start = microtime(true);
for($i = 0; $i < 0xFFFFFF; $i++) $a xor $b;
$end = microtime(true);
echo ($end - $start) / 0xFFFFFF;
2.8898769545694E-8
$ php -r '$a=true; $b=true;
$start = microtime(true);
for($i = 0; $i < 0xFFFFFF; $i++) $a !== $b;
$end = microtime(true);
echo ($end - $start) / 0xFFFFFF;
'
2.735811385077E-8
$ php -r '$a=true; $b=true;
$start = microtime(true);
for($i = 0; $i < 0xFFFFFF; $i++) $a != $b;
$end = microtime(true);
echo ($end - $start) / 0xFFFFFF;
'
3.2480544635878E-8
$ php -r '$a=true; $b=true;
$start = microtime(true);
for($i = 0; $i < 0xFFFFFF; $i++) $a xor $b;
$end = microtime(true);
echo ($end - $start) / 0xFFFFFF;
'
2.9041645487517E-8
$ php -r '$a=true; $b=true;
$start = microtime(true);
for($i = 0; $i < 0xFFFFFF; $i++) $a !== $b;
$end = microtime(true);
echo ($end - $start) / 0xFFFFFF;
'
2.7436373032351E-8
$ php -r '$a=true; $b=true;
$start = microtime(true);
for($i = 0; $i < 0xFFFFFF; $i++) $a != $b;
$end = microtime(true);
echo ($end - $start) / 0xFFFFFF;
'
3.2506237862734E-8
Average:
-
xor
: 28.9 ns -
!==
: 27.4 ns -
!=
: 32.5 ns
I would like to ask:
Why do these operators have such significant performance?
I have actually done these tests a few more times, and !==
is always faster than xor
, and both are significantly (more than 10%) faster than !=
.