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=array(), $count=1) { $time = microtime(1); for($i=0; $i<$count; $i++) { $result = call_user_func_array($function, $args); } 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'); 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/RUjPB
function name:  (null)
number of ops:  25
compiled vars:  !0 = $searchParameters, !1 = $allowedParamters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   ASSIGN                                                   !0, <array>
   38     1        ASSIGN                                                   !1, <array>
   41     2        ECHO                                                     'array_intersect%0A'
   42     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                                                 
   43    13        ECHO                                                     'foreach%0A'
   44    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/RUjPB
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/RUjPB
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 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 9
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 9
Branch analysis from position: 17
Branch analysis from position: 9
filename:       /in/RUjPB
function name:  benchmark
number of ops:  31
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      <array>
          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                                                      ->15
   24     9    >   INIT_USER_CALL                                0          'call_user_func_array', !0
         10        SEND_ARRAY                                               !1
         11        CHECK_UNDEF_ARGS                                         
         12        DO_FCALL                                      0  $9      
         13        ASSIGN                                                   !5, $9
   22    14        PRE_INC                                                  !4
         15    >   IS_SMALLER                                               !4, !2
         16      > JMPNZ                                                    ~12, ->9
   27    17    >   INIT_FCALL                                               'microtime'
         18        SEND_VAL                                                 1
         19        DO_ICALL                                         $13     
         20        SUB                                              ~14     $13, !3
         21        INIT_ARRAY                                       ~15     ~14, 'total_time'
   28    22        INIT_FCALL                                               'microtime'
         23        SEND_VAL                                                 1
         24        DO_ICALL                                         $16     
         25        SUB                                              ~17     $16, !3
         26        DIV                                              ~18     ~17, !2
         27        ADD_ARRAY_ELEMENT                                ~15     ~18, 'average_time'
   29    28        ADD_ARRAY_ELEMENT                                ~15     !2, 'count'
         29      > RETURN                                                   ~15
   31    30*     > RETURN                                                   null

End of function benchmark

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.56 ms | 1407 KiB | 23 Q