duanhuo3392 2016-02-01 14:43
浏览 67
已采纳

PHP XOR性能

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 !=.

  • 写回答

2条回答 默认 最新

  • douchu2823 2016-02-01 15:03
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助