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.
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 nsWhy 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 !=
.
VLD output from PHP 5.6.0 for a simple test of the three options:
$a=true; $b=true;
$x = $a xor $b;
echo '-';
$a=true; $b=true;
$x = $a !== $b;
echo '-';
$a=true; $b=true;
$x = $a != $b;
gives
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename: /in/OLfo9
function name:
number of ops: 16
compiled vars: !0 = $a, !1 = $b, !2 = $x
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
3 0 E > ASSIGN !96,
1 ASSIGN !112,
4 2 ASSIGN $5 !128, !96
3 BOOL_XOR ~6 $5, !112
4 FREE ~6
6 5 ECHO '-'
8 6 ASSIGN !96,
7 ASSIGN !112,
9 8 IS_NOT_IDENTICAL ~9 !96, !112
9 ASSIGN !128, ~9
11 10 ECHO '-'
13 11 ASSIGN !96,
12 ASSIGN !112,
14 13 IS_NOT_EQUAL ~13 !96, !112
14 ASSIGN !128, ~13
15 > RETURN 1
So xor
is indeed creating a temporary result that is subsequently freed