3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('NUMBER_OF_THINGS', 100000); 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; }); } $needle = 50; $start = microtime(true); $result = searchForeach($aBunchOfThings, $needle); $durationForeach = microtime(true) - $start; $start = microtime(true); $result = searchArrayFilter($aBunchOfThings, $needle); $durationArrayFilter = microtime(true) - $start; echo "Function | Time\n"; echo "-----------------------------------------\n"; echo "searchForeach | $durationForeach\n"; echo "searchArrayFilter | $durationArrayFilter\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/8v719
function name:  (null)
number of ops:  63
compiled vars:  !0 = $aBunchOfThings, !1 = $i, !2 = $aThing, !3 = $needle, !4 = $start, !5 = $result, !6 = $durationForeach, !7 = $durationArrayFilter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'define'
          1        SEND_VAL                                                 'NUMBER_OF_THINGS'
          2        SEND_VAL                                                 100000
          3        DO_ICALL                                                 
   16     4        ASSIGN                                                   !0, <array>
   18     5        ASSIGN                                                   !1, 0
          6      > JMP                                                      ->20
   19     7    >   NEW                                              $11     'Something'
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !2, $11
   20    10        INIT_METHOD_CALL                                         !2, 'setField'
         11        INIT_FCALL                                               'rand'
         12        SEND_VAL                                                 1
         13        SEND_VAL                                                 100
         14        DO_ICALL                                         $15     
         15        SEND_VAR_NO_REF_EX                                       $15
         16        DO_FCALL                                      0  $16     
         17        ASSIGN_DIM                                               !0
         18        OP_DATA                                                  $16
   18    19        PRE_INC                                                  !1
         20    >   FETCH_CONSTANT                                   ~18     'NUMBER_OF_THINGS'
         21        IS_SMALLER                                               !1, ~18
         22      > JMPNZ                                                    ~19, ->7
   39    23    >   ASSIGN                                                   !3, 50
   40    24        INIT_FCALL                                               'microtime'
         25        SEND_VAL                                                 <true>
         26        DO_ICALL                                         $21     
         27        ASSIGN                                                   !4, $21
   41    28        INIT_FCALL                                               'searchforeach'
         29        SEND_VAR                                                 !0
         30        SEND_VAR                                                 !3
         31        DO_FCALL                                      0  $23     
         32        ASSIGN                                                   !5, $23
   42    33        INIT_FCALL                                               'microtime'
         34        SEND_VAL                                                 <true>
         35        DO_ICALL                                         $25     
         36        SUB                                              ~26     $25, !4
         37        ASSIGN                                                   !6, ~26
   44    38        INIT_FCALL                                               'microtime'
         39        SEND_VAL                                                 <true>
         40        DO_ICALL                                         $28     
         41        ASSIGN                                                   !4, $28
   45    42        INIT_FCALL                                               'searcharrayfilter'
         43        SEND_VAR                                                 !0
         44        SEND_VAR                                                 !3
         45        DO_FCALL                                      0  $30     
         46        ASSIGN                                                   !5, $30
   46    47        INIT_FCALL                                               'microtime'
         48        SEND_VAL                                                 <true>
         49        DO_ICALL                                         $32     
         50        SUB                                              ~33     $32, !4
         51        ASSIGN                                                   !7, ~33
   48    52        ECHO                                                     'Function++++++++++%7C++Time%0A'
   49    53        ECHO                                                     '-----------------------------------------%0A'
   50    54        ROPE_INIT                                     3  ~36     'searchForeach+++++%7C++'
         55        ROPE_ADD                                      1  ~36     ~36, !6
         56        ROPE_END                                      2  ~35     ~36, '%0A'
         57        ECHO                                                     ~35
   51    58        ROPE_INIT                                     3  ~39     'searchArrayFilter+%7C++'
         59        ROPE_ADD                                      1  ~39     ~39, !7
         60        ROPE_END                                      2  ~38     ~39, '%0A'
         61        ECHO                                                     ~38
   53    62      > 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/8v719
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/8v719
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%2F8v719%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%2F8v719%3A34%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8v719
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%2F8v719%3A34%240

Class Something:
Function setfield:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8v719
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/8v719
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:
157.06 ms | 1410 KiB | 23 Q