3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('NUMBER_OF_THINGS', 10000); class Something{ const SOME_MAX_VALUE = 100; protected $field; public function setField($value){ $this->field = $value; return $this; } public function getField(){ return $this->field; } } $aBunchOfThings = array(); for($i=0; $i<NUMBER_OF_THINGS; $i++){ $aThing = new Something(); $aBunchOfThings[] = $aThing->setField(rand(1, Something::SOME_MAX_VALUE)); } function searchForeach($array, $needle){ $results = array(); foreach($array as $key => $thing){ if($thing->getField() == $needle){ $result[] = $needle; } } return $result; } function searchArrayFilter($array, $needle){ return array_filter($array, function(&$thing) use (&$needle){ return $thing->getField() == $needle; }); } function uniqueForeach($array){ $results = array(); foreach($array as $key => $thing){ $uniqueThing = true; foreach($results as $result){ if($result->getField() == $thing->getField()){ $uniqueThing = false; break; } } if($uniqueThing){ $result[] = $thing; } } return $result; } function uniqueArrayMap($array){ return array_intersect_key($array, array_unique(array_map(function (&$thing) { return $thing->getField(); }, $array))); } $needle = 50; $start = microtime(true); $result = searchForeach($aBunchOfThings, $needle); $durationForeach = microtime(true) - $start; $start = microtime(true); $result = searchArrayFilter($aBunchOfThings, $needle); $durationArrayFilter = microtime(true) - $start; $start = microtime(true); $result = uniqueForeach($aBunchOfThings); $durationUniqueForeach = microtime(true) - $start; $start = microtime(true); $result = uniqueArrayMap($aBunchOfThings); $durationUniqueArrayMap = microtime(true) - $start; echo "Function | Time\n"; echo "-----------------------------------------\n"; echo "searchForeach | $durationForeach\n"; echo "searchArrayFilter | $durationArrayFilter\n"; echo "uniqueForeach | $durationUniqueForeach\n"; echo "uniqueArrayMap | $durationUniqueArrayMap\n"; ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 7
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 7
Branch analysis from position: 23
Branch analysis from position: 7
filename:       /in/7ckVF
function name:  (null)
number of ops:  97
compiled vars:  !0 = $aBunchOfThings, !1 = $i, !2 = $aThing, !3 = $needle, !4 = $start, !5 = $result, !6 = $durationForeach, !7 = $durationArrayFilter, !8 = $durationUniqueForeach, !9 = $durationUniqueArrayMap
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'define'
          1        SEND_VAL                                                 'NUMBER_OF_THINGS'
          2        SEND_VAL                                                 10000
          3        DO_ICALL                                                 
   16     4        ASSIGN                                                   !0, <array>
   18     5        ASSIGN                                                   !1, 0
          6      > JMP                                                      ->20
   19     7    >   NEW                                              $13     'Something'
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !2, $13
   20    10        INIT_METHOD_CALL                                         !2, 'setField'
         11        INIT_FCALL                                               'rand'
         12        SEND_VAL                                                 1
         13        SEND_VAL                                                 100
         14        DO_ICALL                                         $17     
         15        SEND_VAR_NO_REF_EX                                       $17
         16        DO_FCALL                                      0  $18     
         17        ASSIGN_DIM                                               !0
         18        OP_DATA                                                  $18
   18    19        PRE_INC                                                  !1
         20    >   FETCH_CONSTANT                                   ~20     'NUMBER_OF_THINGS'
         21        IS_SMALLER                                               !1, ~20
         22      > JMPNZ                                                    ~21, ->7
   62    23    >   ASSIGN                                                   !3, 50
   63    24        INIT_FCALL                                               'microtime'
         25        SEND_VAL                                                 <true>
         26        DO_ICALL                                         $23     
         27        ASSIGN                                                   !4, $23
   64    28        INIT_FCALL                                               'searchforeach'
         29        SEND_VAR                                                 !0
         30        SEND_VAR                                                 !3
         31        DO_FCALL                                      0  $25     
         32        ASSIGN                                                   !5, $25
   65    33        INIT_FCALL                                               'microtime'
         34        SEND_VAL                                                 <true>
         35        DO_ICALL                                         $27     
         36        SUB                                              ~28     $27, !4
         37        ASSIGN                                                   !6, ~28
   67    38        INIT_FCALL                                               'microtime'
         39        SEND_VAL                                                 <true>
         40        DO_ICALL                                         $30     
         41        ASSIGN                                                   !4, $30
   68    42        INIT_FCALL                                               'searcharrayfilter'
         43        SEND_VAR                                                 !0
         44        SEND_VAR                                                 !3
         45        DO_FCALL                                      0  $32     
         46        ASSIGN                                                   !5, $32
   69    47        INIT_FCALL                                               'microtime'
         48        SEND_VAL                                                 <true>
         49        DO_ICALL                                         $34     
         50        SUB                                              ~35     $34, !4
         51        ASSIGN                                                   !7, ~35
   71    52        INIT_FCALL                                               'microtime'
         53        SEND_VAL                                                 <true>
         54        DO_ICALL                                         $37     
         55        ASSIGN                                                   !4, $37
   72    56        INIT_FCALL                                               'uniqueforeach'
         57        SEND_VAR                                                 !0
         58        DO_FCALL                                      0  $39     
         59        ASSIGN                                                   !5, $39
   73    60        INIT_FCALL                                               'microtime'
         61        SEND_VAL                                                 <true>
         62        DO_ICALL                                         $41     
         63        SUB                                              ~42     $41, !4
         64        ASSIGN                                                   !8, ~42
   75    65        INIT_FCALL                                               'microtime'
         66        SEND_VAL                                                 <true>
         67        DO_ICALL                                         $44     
         68        ASSIGN                                                   !4, $44
   76    69        INIT_FCALL                                               'uniquearraymap'
         70        SEND_VAR                                                 !0
         71        DO_FCALL                                      0  $46     
         72        ASSIGN                                                   !5, $46
   77    73        INIT_FCALL                                               'microtime'
         74        SEND_VAL                                                 <true>
         75        DO_ICALL                                         $48     
         76        SUB                                              ~49     $48, !4
         77        ASSIGN                                                   !9, ~49
   79    78        ECHO                                                     'Function++++++++++%7C++Time%0A'
   80    79        ECHO                                                     '-----------------------------------------%0A'
   81    80        ROPE_INIT                                     3  ~52     'searchForeach+++++%7C++'
         81        ROPE_ADD                                      1  ~52     ~52, !6
         82        ROPE_END                                      2  ~51     ~52, '%0A'
         83        ECHO                                                     ~51
   82    84        ROPE_INIT                                     3  ~55     'searchArrayFilter+%7C++'
         85        ROPE_ADD                                      1  ~55     ~55, !7
         86        ROPE_END                                      2  ~54     ~55, '%0A'
         87        ECHO                                                     ~54
   83    88        ROPE_INIT                                     3  ~58     'uniqueForeach+++++%7C++'
         89        ROPE_ADD                                      1  ~58     ~58, !8
         90        ROPE_END                                      2  ~57     ~58, '%0A'
         91        ECHO                                                     ~57
   84    92        ROPE_INIT                                     3  ~61     'uniqueArrayMap++++%7C++'
         93        ROPE_ADD                                      1  ~61     ~61, !9
         94        ROPE_END                                      2  ~60     ~61, '%0A'
         95        ECHO                                                     ~60
   86    96      > RETURN                                                   1

Function searchforeach:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/7ckVF
function name:  searchForeach
number of ops:  16
compiled vars:  !0 = $array, !1 = $needle, !2 = $results, !3 = $thing, !4 = $key, !5 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   24     2        ASSIGN                                                   !2, <array>
   25     3      > FE_RESET_R                                       $7      !0, ->13
          4    > > FE_FETCH_R                                       ~8      $7, !3, ->13
          5    >   ASSIGN                                                   !4, ~8
   26     6        INIT_METHOD_CALL                                         !3, 'getField'
          7        DO_FCALL                                      0  $10     
          8        IS_EQUAL                                                 !1, $10
          9      > JMPZ                                                     ~11, ->12
   27    10    >   ASSIGN_DIM                                               !5
         11        OP_DATA                                                  !1
   25    12    > > JMP                                                      ->4
         13    >   FE_FREE                                                  $7
   30    14      > RETURN                                                   !5
   31    15*     > RETURN                                                   null

End of function searchforeach

Function searcharrayfilter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7ckVF
function name:  searchArrayFilter
number of ops:  10
compiled vars:  !0 = $array, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F7ckVF%3A34%240'
          5        BIND_LEXICAL                                             ~2, !1
   36     6        SEND_VAL                                                 ~2
          7        DO_ICALL                                         $3      
          8      > RETURN                                                   $3
   37     9*     > RETURN                                                   null

End of function searcharrayfilter

Function %00%7Bclosure%7D%2Fin%2F7ckVF%3A34%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7ckVF
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $thing, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   35     2        INIT_METHOD_CALL                                         !0, 'getField'
          3        DO_FCALL                                      0  $2      
          4        IS_EQUAL                                         ~3      !1, $2
          5      > RETURN                                                   ~3
   36     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F7ckVF%3A34%240

Function uniqueforeach:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 22
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 22
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 21
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 17
Branch analysis from position: 17
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/7ckVF
function name:  uniqueForeach
number of ops:  25
compiled vars:  !0 = $array, !1 = $results, !2 = $thing, !3 = $key, !4 = $uniqueThing, !5 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   40     1        ASSIGN                                                   !1, <array>
   41     2      > FE_RESET_R                                       $7      !0, ->22
          3    > > FE_FETCH_R                                       ~8      $7, !2, ->22
          4    >   ASSIGN                                                   !3, ~8
   42     5        ASSIGN                                                   !4, <true>
   43     6      > FE_RESET_R                                       $11     !1, ->17
          7    > > FE_FETCH_R                                               $11, !5, ->17
   44     8    >   INIT_METHOD_CALL                                         !5, 'getField'
          9        DO_FCALL                                      0  $12     
         10        INIT_METHOD_CALL                                         !2, 'getField'
         11        DO_FCALL                                      0  $13     
         12        IS_EQUAL                                                 $12, $13
         13      > JMPZ                                                     ~14, ->16
   45    14    >   ASSIGN                                                   !4, <false>
   46    15      > JMP                                                      ->17
   43    16    > > JMP                                                      ->7
         17    >   FE_FREE                                                  $11
   49    18      > JMPZ                                                     !4, ->21
   50    19    >   ASSIGN_DIM                                               !5
         20        OP_DATA                                                  !2
   41    21    > > JMP                                                      ->3
         22    >   FE_FREE                                                  $7
   53    23      > RETURN                                                   !5
   54    24*     > RETURN                                                   null

End of function uniqueforeach

Function uniquearraymap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7ckVF
function name:  uniqueArrayMap
number of ops:  15
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
   57     1        INIT_FCALL                                               'array_intersect_key'
          2        SEND_VAR                                                 !0
          3        INIT_FCALL                                               'array_unique'
          4        INIT_FCALL                                               'array_map'
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F7ckVF%3A57%241'
   59     6        SEND_VAL                                                 ~1
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $2      
          9        SEND_VAR                                                 $2
         10        DO_ICALL                                         $3      
         11        SEND_VAR                                                 $3
         12        DO_ICALL                                         $4      
         13      > RETURN                                                   $4
   60    14*     > RETURN                                                   null

End of function uniquearraymap

Function %00%7Bclosure%7D%2Fin%2F7ckVF%3A57%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7ckVF
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $thing
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   58     1        INIT_METHOD_CALL                                         !0, 'getField'
          2        DO_FCALL                                      0  $1      
          3      > RETURN                                                   $1
   59     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F7ckVF%3A57%241

Class Something:
Function setfield:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7ckVF
function name:  setField
number of ops:  6
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     1        ASSIGN_OBJ                                               'field'
          2        OP_DATA                                                  !0
    9     3        FETCH_THIS                                       ~2      
          4      > RETURN                                                   ~2
   10     5*     > RETURN                                                   null

End of function setfield

Function getfield:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7ckVF
function name:  getField
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   FETCH_OBJ_R                                      ~0      'field'
          1      > RETURN                                                   ~0
   13     2*     > RETURN                                                   null

End of function getfield

End of class Something.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.21 ms | 964 KiB | 32 Q