3v4l.org

run code in 300+ PHP versions simultaneously
<?php function one($s, $p) { return array_intersect_key($s, array_flip($p)); } function two($s, $p) { foreach($s as $key => $value) { if(!array_key_exists($key, $p)) { unset($s[$key]); } } return $s; } function benchmark(callable $function, $args=null, $count=1) { $time = microtime(1); for($i=0; $i<$count; $i++) { $result = is_array($args)? call_user_func_array($function, $args): call_user_func_array($function); } return [ 'total_time' => microtime(1) - $time, 'average_time' => (microtime(1) - $time)/$count, 'count' => $count ]; } $searchParameters = array( 'parameter1' => 'value1', 'parameter2' => 'value2', 'parameter3' => 'value3', ); $allowedParamters = array('parameter1','parameter2'); //set_time_limit(0); echo('array_intersect'.PHP_EOL); var_dump(benchmark('one', [$searchParameters, $allowedParamters], 1E6)); echo('foreach'.PHP_EOL); var_dump(benchmark('two', [$searchParameters, $allowedParamters], 1E6));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XMDKI
function name:  (null)
number of ops:  25
compiled vars:  !0 = $searchParameters, !1 = $allowedParamters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   40     1        ASSIGN                                                   !1, <array>
   43     2        ECHO                                                     'array_intersect%0A'
   44     3        INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'benchmark'
          5        SEND_VAL                                                 'one'
          6        INIT_ARRAY                                       ~4      !0
          7        ADD_ARRAY_ELEMENT                                ~4      !1
          8        SEND_VAL                                                 ~4
          9        SEND_VAL                                                 1.0e+6
         10        DO_FCALL                                      0  $5      
         11        SEND_VAR                                                 $5
         12        DO_ICALL                                                 
   45    13        ECHO                                                     'foreach%0A'
   46    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'benchmark'
         16        SEND_VAL                                                 'two'
         17        INIT_ARRAY                                       ~7      !0
         18        ADD_ARRAY_ELEMENT                                ~7      !1
         19        SEND_VAL                                                 ~7
         20        SEND_VAL                                                 1.0e+6
         21        DO_FCALL                                      0  $8      
         22        SEND_VAR                                                 $8
         23        DO_ICALL                                                 
         24      > RETURN                                                   1

Function one:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XMDKI
function name:  one
number of ops:  11
compiled vars:  !0 = $s, !1 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        INIT_FCALL                                               'array_intersect_key'
          3        SEND_VAR                                                 !0
          4        INIT_FCALL                                               'array_flip'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                         $3      
          9      > RETURN                                                   $3
    5    10*     > RETURN                                                   null

End of function one

Function two:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/XMDKI
function name:  two
number of ops:  13
compiled vars:  !0 = $s, !1 = $p, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2      > FE_RESET_R                                       $4      !0, ->10
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->10
          4    >   ASSIGN                                                   !3, ~5
   11     5        ARRAY_KEY_EXISTS                                 ~7      !3, !1
          6        BOOL_NOT                                         ~8      ~7
          7      > JMPZ                                                     ~8, ->9
   13     8    >   UNSET_DIM                                                !0, !3
    9     9    > > JMP                                                      ->3
         10    >   FE_FREE                                                  $4
   16    11      > RETURN                                                   !0
   17    12*     > RETURN                                                   null

End of function two

Function benchmark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 9
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 17
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 9
Branch analysis from position: 25
Branch analysis from position: 9
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 9
Branch analysis from position: 25
Branch analysis from position: 9
filename:       /in/XMDKI
function name:  benchmark
number of ops:  39
compiled vars:  !0 = $function, !1 = $args, !2 = $count, !3 = $time, !4 = $i, !5 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      1
   21     3        INIT_FCALL                                               'microtime'
          4        SEND_VAL                                                 1
          5        DO_ICALL                                         $6      
          6        ASSIGN                                                   !3, $6
   22     7        ASSIGN                                                   !4, 0
          8      > JMP                                                      ->23
   24     9    >   TYPE_CHECK                                  128          !1
         10      > JMPZ                                                     ~9, ->17
   25    11    >   INIT_USER_CALL                                0          'call_user_func_array', !0
         12        SEND_ARRAY                                               !1
         13        CHECK_UNDEF_ARGS                                         
         14        DO_FCALL                                      0  $10     
         15        QM_ASSIGN                                        ~11     $10
         16      > JMP                                                      ->21
   26    17    >   INIT_FCALL                                               'call_user_func_array'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $12     
         20        QM_ASSIGN                                        ~11     $12
   24    21    >   ASSIGN                                                   !5, ~11
   22    22        PRE_INC                                                  !4
         23    >   IS_SMALLER                                               !4, !2
         24      > JMPNZ                                                    ~15, ->9
   29    25    >   INIT_FCALL                                               'microtime'
         26        SEND_VAL                                                 1
         27        DO_ICALL                                         $16     
         28        SUB                                              ~17     $16, !3
         29        INIT_ARRAY                                       ~18     ~17, 'total_time'
   30    30        INIT_FCALL                                               'microtime'
         31        SEND_VAL                                                 1
         32        DO_ICALL                                         $19     
         33        SUB                                              ~20     $19, !3
         34        DIV                                              ~21     ~20, !2
         35        ADD_ARRAY_ELEMENT                                ~18     ~21, 'average_time'
   31    36        ADD_ARRAY_ELEMENT                                ~18     !2, 'count'
         37      > RETURN                                                   ~18
   33    38*     > RETURN                                                   null

End of function benchmark

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.36 ms | 1407 KiB | 25 Q