What happens under the hood of these conditions?

Using the switch:

switch($page){
    case 'dog':
    case 'cat':
    case 'robot':
    case 'default':{

        break;
    };
    default:{

        break;
    }
}

Using if:

if($page == 'dog' || $page == 'cat' || $page == 'robot' || $page == 'default'){

}else{

}

Using a needle and haystack:

$pages = array('dog', 'cat', 'robot', 'default');
if(in_array($page, $pages)){

}else{

}

I would like to know what is happening under the hood.


average speed:

  • arrow Runtime: 0.054877042770386 seconds
  • if Runtime: 0.014014959335327 seconds
  • switch Runtime: 0.0093550682067871 seconds
+4
source share
1 answer

Output all scripts with VLD (PHP 5.6.4)

Switch

number of ops:  31
compiled vars:  !0 = $page
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   EXT_STMT                                                 
         1        ASSIGN                                                   !0, 'default'
   3     2        NOP                                                      
   4     3        EXT_STMT                                                 
         4        CASE                                             ~1      !0, 'dog'
         5      > JMPZ                                                     ~1, ->7
   5     6    > > JMP                                                      ->10
         7    >   EXT_STMT                                                 
         8        CASE                                             ~1      !0, 'cat'
         9      > JMPZ                                                     ~1, ->11
   6    10    > > JMP                                                      ->14
        11    >   EXT_STMT                                                 
        12        CASE                                             ~1      !0, 'robot'
        13      > JMPZ                                                     ~1, ->15
   7    14    > > JMP                                                      ->18
        15    >   EXT_STMT                                                 
        16        CASE                                             ~1      !0, 'default'
        17      > JMPZ                                                     ~1, ->23
        18    >   NOP                                                      
   9    19        EXT_STMT                                                 
        20      > BRK                                                      1, ->30
  11    21*       EXT_STMT                                                 
        22*       JMP                                                      ->25
        23    >   EXT_STMT                                                 
        24      > JMP                                                      ->29
        25    >   NOP                                                      
  13    26        EXT_STMT                                                 
        27      > BRK                                                      1, ->30
  15    28*       JMP                                                      ->30
        29    > > JMP                                                      ->25
  16    30    > > RETURN                                                   1

, if a

number of ops:  16
compiled vars:  !0 = $page
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   EXT_STMT                                                 
         1        ASSIGN                                                   !0, 'default'
   3     2        EXT_STMT                                                 
         3        IS_EQUAL                                         ~1      !0, 'dog'
         4      > JMPNZ_EX                                         ~1      ~1, ->7
         5    >   IS_EQUAL                                         ~2      !0, 'cat'
         6        BOOL                                             ~1      ~2
         7    > > JMPNZ_EX                                         ~1      ~1, ->10
         8    >   IS_EQUAL                                         ~3      !0, 'robot'
         9        BOOL                                             ~1      ~3
        10    > > JMPNZ_EX                                         ~1      ~1, ->13
        11    >   IS_EQUAL                                         ~4      !0, 'default'
        12        BOOL                                             ~1      ~4
        13    > > JMPZ                                                     ~1, ->15
   5    14    > > JMP                                                      ->15

needle and haystacks

number of ops:  17
compiled vars:  !0 = $page, !1 = $pages
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   EXT_STMT                                                 
         1        ASSIGN                                                   !0, 'default'
   3     2        EXT_STMT                                                 
         3        INIT_ARRAY                                       ~1      'dog'
         4        ADD_ARRAY_ELEMENT                                ~1      'cat'
         5        ADD_ARRAY_ELEMENT                                ~1      'robot'
         6        ADD_ARRAY_ELEMENT                                ~1      'default'
         7        ASSIGN                                                   !1, ~1
   4     8        EXT_STMT                                                 
         9        EXT_FCALL_BEGIN                                          
        10        SEND_VAR                                                 !0
        11        SEND_VAR                                                 !1
        12        DO_FCALL                                      2  $3      'in_array'
        13        EXT_FCALL_END                                            
        14      > JMPZ                                                     $3, ->16
   6    15    > > JMP                                                      ->16
   9    16    > > RETURN                                                   1

, , ( , , "" )

, .

alot CASE (, 2 ) JMPZ (Jump, ).

IS_EQUAL, , , . , , 2 , . ( , , )

. PHP https://github.com/php/php-src/blob/df29df7ec40cf7950a98f36bfa99ef19f0950309/ext/standard/array.c#L1595

C , . , ,

https://github.com/php/php-src/blob/213b49620d24ebebe3cf19787ee6e3448d27d046/Zend/zend_operators.h#L712

, .

script

$max_checks = 1000000;

$bench = -microtime(true);
for($i = 0;$i < $max_checks; $i++) {
  switch($page){
    case 'dog':
    case 'cat':
    case 'robot':
    case 'default':{
       break;
     };
    default:{

      break;
    }
  }
}
echo "\nSwitch took " . (microtime(true) + $bench);

$bench = -microtime(true);


for($i = 0;$i < $max_checks; $i++) {
  $page == 'dog' || $page == 'cat' || $page == 'robot' || $page == 'default';
}
echo "\nif took " . (microtime(true) + $bench);


$pages = array('dog', 'cat', 'robot', 'default');

$bench = -microtime(true);
for($i = 0;$i < $max_checks; $i++) {
  (in_array($page, $pages));
}

echo "\nneedle haystack took " . (microtime(true) + $bench);

$page = 'dog';

Switch took 0.31698203086853
if took 0.18721604347229
needle haystack took 1.5701420307159

$page = 'default';

Switch took 0.46866297721863
if took 0.40072298049927
needle haystack took 1.6747360229492

$page = 'no match';

Switch took 0.52629804611206
if took 0.40276217460632
needle haystack took 1.6838929653168

, , ( )

Edit:

, . if ===

IS_EQUAL IS_IDENTICAL

if      took 0.43217587471008
if(===) took 0.39284706115723

http://php.net/manual/en/internals2.opcodes.list.php

2

im PHP 7 RC3 .

( )

$page = 'dog';

Switch took 0.018393993377686
if took 0.030646085739136
if(===) took 0.036449909210205
needle haystack took 0.045974969863892

$page = 'default';

Switch took 0.040921211242676
if took 0.085216999053955
if(===) took 0.1043848991394
needle haystack took 0.052649974822998

$page = 'no match';

Switch took 0.059795141220093
if took 0.080615997314453
if(===) took 0.10486197471619
needle haystack took 0.049200057983398

PHP 7

+4

Source: https://habr.com/ru/post/1607886/


All Articles