3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** leavecalc Copyright (C) 2014 Paul White This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ //Access every element once using array_filter() $objectArray = []; $objectNames = []; for($i = 0; $i < 1000; $i ++){ $objName = 'object_name_' . ($i + 1); $objectNames[] = $objName; $obj = new stdClass(); $obj->name = $objName; $obj->description = 'test description'; $obj->accessed = 0; $objectArray[] = $obj; } $start = microtime(true); foreach($objectNames as $name){ $iterations = getObjectWithArray_Filter($name, $objectArray); } $end = microtime(true); $taken = $end - $start; echo "Accessing 1000 elements once took " . $iterations . " iterations using array_filter() in $taken seconds<br/>\n"; //Access every element once using foreach(){} $objectArray = []; $objectNames = []; for($i = 0; $i < 1000; $i ++){ $objName = 'object_name_' . ($i + 1); $objectNames[] = $objName; $obj = new stdClass(); $obj->name = $objName; $obj->description = 'test description'; $obj->accessed = 0; $objectArray[] = $obj; } $start = microtime(true); foreach($objectNames as $name){ $iterations = getObjectWithForeach($name, $objectArray); } $end = microtime(true); $taken = $end - $start; echo "Accessing 1000 elements once took " . $iterations . " iterations using foreach(){} in $taken seconds<br/>\n"; //Access every element once using Associative array $objectArray = []; $objectNames = []; for($i = 0; $i < 1000; $i ++){ $objName = 'object_name_' . ($i + 1); $objectNames[] = $objName; $obj = new stdClass(); $obj->name = $objName; $obj->description = 'test description'; $obj->accessed = 0; $objectArray[] = $obj; } $associativeArray = []; $start = microtime(true); foreach($objectArray as $object){ $associativeArray[$object->name] = $object; $object->accessed ++; } foreach($objectNames as $name){ $iterations = getObjectFromAssociativeArray($objName, $associativeArray); } $end = microtime(true); $taken = $end - $start; echo "Accessing 1000 elements once took " . $iterations . " iterations using associative array{} in $taken seconds<br/>\n"; //================================================================= function getObjectWithArray_Filter($objectName, array $objectArray){ $myobjects = array_filter($objectArray, function($e) use($objectName) { $e->accessed ++; return strcmp($e->name, $objectName) == 0; }); $iterations = 0; foreach($objectArray as $object){ $iterations += $object->accessed; } return $iterations; } function getObjectWithForeach($objectName, array $objectArray){ $iterations = 0; $found = false; $count = 0; while(!$found){ $objectArray[$count]->accessed ++; if($objectArray[$count]->name === $objectName){ $found = true; } $count ++; } foreach($objectArray as $object){ $iterations += $object->accessed; } return $iterations; } function getObjectFromAssociativeArray($objectName, array $objectArray){ $iterations = 0; if($objectName === $objectArray[$objectName]->name){ $objectArray[$objectName]->accessed ++; } foreach($objectArray as $object){ $iterations += $object->accessed; } return $iterations; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 4
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 35
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 52
Branch analysis from position: 71
2 jumps found. (Code = 77) Position 1 = 76, Position 2 = 83
Branch analysis from position: 76
2 jumps found. (Code = 78) Position 1 = 77, Position 2 = 83
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
Branch analysis from position: 83
1 jumps found. (Code = 42) Position 1 = 117
Branch analysis from position: 117
2 jumps found. (Code = 44) Position 1 = 119, Position 2 = 100
Branch analysis from position: 119
2 jumps found. (Code = 77) Position 1 = 125, Position 2 = 131
Branch analysis from position: 125
2 jumps found. (Code = 78) Position 1 = 126, Position 2 = 131
Branch analysis from position: 126
1 jumps found. (Code = 42) Position 1 = 125
Branch analysis from position: 125
Branch analysis from position: 131
2 jumps found. (Code = 77) Position 1 = 133, Position 2 = 140
Branch analysis from position: 133
2 jumps found. (Code = 78) Position 1 = 134, Position 2 = 140
Branch analysis from position: 134
1 jumps found. (Code = 42) Position 1 = 133
Branch analysis from position: 133
Branch analysis from position: 140
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 140
Branch analysis from position: 131
Branch analysis from position: 100
2 jumps found. (Code = 44) Position 1 = 119, Position 2 = 100
Branch analysis from position: 119
Branch analysis from position: 100
Branch analysis from position: 83
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 52
Branch analysis from position: 71
Branch analysis from position: 52
Branch analysis from position: 35
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 4
Branch analysis from position: 23
Branch analysis from position: 4
filename:       /in/DWDvB
function name:  (null)
number of ops:  154
compiled vars:  !0 = $objectArray, !1 = $objectNames, !2 = $i, !3 = $objName, !4 = $obj, !5 = $start, !6 = $name, !7 = $iterations, !8 = $end, !9 = $taken, !10 = $associativeArray, !11 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   ASSIGN                                                   !0, <array>
   22     1        ASSIGN                                                   !1, <array>
   23     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->21
   24     4    >   ADD                                              ~15     !2, 1
          5        CONCAT                                           ~16     'object_name_', ~15
          6        ASSIGN                                                   !3, ~16
   25     7        ASSIGN_DIM                                               !1
          8        OP_DATA                                                  !3
   26     9        NEW                                              $19     'stdClass'
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !4, $19
   27    12        ASSIGN_OBJ                                               !4, 'name'
         13        OP_DATA                                                  !3
   28    14        ASSIGN_OBJ                                               !4, 'description'
         15        OP_DATA                                                  'test+description'
   29    16        ASSIGN_OBJ                                               !4, 'accessed'
         17        OP_DATA                                                  0
   30    18        ASSIGN_DIM                                               !0
         19        OP_DATA                                                  !4
   23    20        PRE_INC                                                  !2
         21    >   IS_SMALLER                                               !2, 1000
         22      > JMPNZ                                                    ~27, ->4
   32    23    >   INIT_FCALL                                               'microtime'
         24        SEND_VAL                                                 <true>
         25        DO_ICALL                                         $28     
         26        ASSIGN                                                   !5, $28
   33    27      > FE_RESET_R                                       $30     !1, ->35
         28    > > FE_FETCH_R                                               $30, !6, ->35
   34    29    >   INIT_FCALL_BY_NAME                                       'getObjectWithArray_Filter'
         30        SEND_VAR_EX                                              !6
         31        SEND_VAR_EX                                              !0
         32        DO_FCALL                                      0  $31     
         33        ASSIGN                                                   !7, $31
   33    34      > JMP                                                      ->28
         35    >   FE_FREE                                                  $30
   36    36        INIT_FCALL                                               'microtime'
         37        SEND_VAL                                                 <true>
         38        DO_ICALL                                         $33     
         39        ASSIGN                                                   !8, $33
   37    40        SUB                                              ~35     !8, !5
         41        ASSIGN                                                   !9, ~35
   38    42        CONCAT                                           ~37     'Accessing+1000+elements+once+took+', !7
         43        ROPE_INIT                                     3  ~39     '+iterations+using+array_filter%28%29+in+'
         44        ROPE_ADD                                      1  ~39     ~39, !9
         45        ROPE_END                                      2  ~38     ~39, '+seconds%3Cbr%2F%3E%0A'
         46        CONCAT                                           ~41     ~37, ~38
         47        ECHO                                                     ~41
   42    48        ASSIGN                                                   !0, <array>
   43    49        ASSIGN                                                   !1, <array>
   44    50        ASSIGN                                                   !2, 0
         51      > JMP                                                      ->69
   45    52    >   ADD                                              ~45     !2, 1
         53        CONCAT                                           ~46     'object_name_', ~45
         54        ASSIGN                                                   !3, ~46
   46    55        ASSIGN_DIM                                               !1
         56        OP_DATA                                                  !3
   47    57        NEW                                              $49     'stdClass'
         58        DO_FCALL                                      0          
         59        ASSIGN                                                   !4, $49
   48    60        ASSIGN_OBJ                                               !4, 'name'
         61        OP_DATA                                                  !3
   49    62        ASSIGN_OBJ                                               !4, 'description'
         63        OP_DATA                                                  'test+description'
   50    64        ASSIGN_OBJ                                               !4, 'accessed'
         65        OP_DATA                                                  0
   51    66        ASSIGN_DIM                                               !0
         67        OP_DATA                                                  !4
   44    68        PRE_INC                                                  !2
         69    >   IS_SMALLER                                               !2, 1000
         70      > JMPNZ                                                    ~57, ->52
   54    71    >   INIT_FCALL                                               'microtime'
         72        SEND_VAL                                                 <true>
         73        DO_ICALL                                         $58     
         74        ASSIGN                                                   !5, $58
   55    75      > FE_RESET_R                                       $60     !1, ->83
         76    > > FE_FETCH_R                                               $60, !6, ->83
   56    77    >   INIT_FCALL_BY_NAME                                       'getObjectWithForeach'
         78        SEND_VAR_EX                                              !6
         79        SEND_VAR_EX                                              !0
         80        DO_FCALL                                      0  $61     
         81        ASSIGN                                                   !7, $61
   55    82      > JMP                                                      ->76
         83    >   FE_FREE                                                  $60
   58    84        INIT_FCALL                                               'microtime'
         85        SEND_VAL                                                 <true>
         86        DO_ICALL                                         $63     
         87        ASSIGN                                                   !8, $63
   59    88        SUB                                              ~65     !8, !5
         89        ASSIGN                                                   !9, ~65
   60    90        CONCAT                                           ~67     'Accessing+1000+elements+once+took+', !7
         91        ROPE_INIT                                     3  ~69     '+iterations+using+foreach%28%29%7B%7D+in+'
         92        ROPE_ADD                                      1  ~69     ~69, !9
         93        ROPE_END                                      2  ~68     ~69, '+seconds%3Cbr%2F%3E%0A'
         94        CONCAT                                           ~71     ~67, ~68
         95        ECHO                                                     ~71
   63    96        ASSIGN                                                   !0, <array>
   64    97        ASSIGN                                                   !1, <array>
   65    98        ASSIGN                                                   !2, 0
         99      > JMP                                                      ->117
   66   100    >   ADD                                              ~75     !2, 1
        101        CONCAT                                           ~76     'object_name_', ~75
        102        ASSIGN                                                   !3, ~76
   67   103        ASSIGN_DIM                                               !1
        104        OP_DATA                                                  !3
   68   105        NEW                                              $79     'stdClass'
        106        DO_FCALL                                      0          
        107        ASSIGN                                                   !4, $79
   69   108        ASSIGN_OBJ                                               !4, 'name'
        109        OP_DATA                                                  !3
   70   110        ASSIGN_OBJ                                               !4, 'description'
        111        OP_DATA                                                  'test+description'
   71   112        ASSIGN_OBJ                                               !4, 'accessed'
        113        OP_DATA                                                  0
   72   114        ASSIGN_DIM                                               !0
        115        OP_DATA                                                  !4
   65   116        PRE_INC                                                  !2
        117    >   IS_SMALLER                                               !2, 1000
        118      > JMPNZ                                                    ~87, ->100
   75   119    >   ASSIGN                                                   !10, <array>
   76   120        INIT_FCALL                                               'microtime'
        121        SEND_VAL                                                 <true>
        122        DO_ICALL                                         $89     
        123        ASSIGN                                                   !5, $89
   77   124      > FE_RESET_R                                       $91     !0, ->131
        125    > > FE_FETCH_R                                               $91, !11, ->131
   78   126    >   FETCH_OBJ_R                                      ~92     !11, 'name'
        127        ASSIGN_DIM                                               !10, ~92
        128        OP_DATA                                                  !11
   79   129        PRE_INC_OBJ                                              !11, 'accessed'
   77   130      > JMP                                                      ->125
        131    >   FE_FREE                                                  $91
   82   132      > FE_RESET_R                                       $95     !1, ->140
        133    > > FE_FETCH_R                                               $95, !6, ->140
   83   134    >   INIT_FCALL_BY_NAME                                       'getObjectFromAssociativeArray'
        135        SEND_VAR_EX                                              !3
        136        SEND_VAR_EX                                              !10
        137        DO_FCALL                                      0  $96     
        138        ASSIGN                                                   !7, $96
   82   139      > JMP                                                      ->133
        140    >   FE_FREE                                                  $95
   85   141        INIT_FCALL                                               'microtime'
        142        SEND_VAL                                                 <true>
        143        DO_ICALL                                         $98     
        144        ASSIGN                                                   !8, $98
   86   145        SUB                                              ~100    !8, !5
        146        ASSIGN                                                   !9, ~100
   87   147        CONCAT                                           ~102    'Accessing+1000+elements+once+took+', !7
        148        ROPE_INIT                                     3  ~104    '+iterations+using+associative+array%7B%7D+in+'
        149        ROPE_ADD                                      1  ~104    ~104, !9
        150        ROPE_END                                      2  ~103    ~104, '+seconds%3Cbr%2F%3E%0A'
        151        CONCAT                                           ~106    ~102, ~103
        152        ECHO                                                     ~106
  128   153      > RETURN                                                   1

Function getobjectwitharray_filter:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/DWDvB
function name:  getObjectWithArray_Filter
number of ops:  18
compiled vars:  !0 = $objectName, !1 = $objectArray, !2 = $myobjects, !3 = $iterations, !4 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   91     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !1
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDWDvB%3A91%240'
          5        BIND_LEXICAL                                             ~5, !0
   94     6        SEND_VAL                                                 ~5
          7        DO_ICALL                                         $6      
   91     8        ASSIGN                                                   !2, $6
   95     9        ASSIGN                                                   !3, 0
   96    10      > FE_RESET_R                                       $9      !1, ->15
         11    > > FE_FETCH_R                                               $9, !4, ->15
   97    12    >   FETCH_OBJ_R                                      ~10     !4, 'accessed'
         13        ASSIGN_OP                                     1          !3, ~10
   96    14      > JMP                                                      ->11
         15    >   FE_FREE                                                  $9
   99    16      > RETURN                                                   !3
  100    17*     > RETURN                                                   null

End of function getobjectwitharray_filter

Function %00%7Bclosure%7D%2Fin%2FDWDvB%3A91%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DWDvB
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $e, !1 = $objectName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   92     2        PRE_INC_OBJ                                              !0, 'accessed'
   93     3        INIT_FCALL                                               'strcmp'
          4        FETCH_OBJ_R                                      ~3      !0, 'name'
          5        SEND_VAL                                                 ~3
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $4      
          8        IS_EQUAL                                         ~5      $4, 0
          9      > RETURN                                                   ~5
   94    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDWDvB%3A91%240

Function getobjectwithforeach:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 6
Branch analysis from position: 16
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 6
Branch analysis from position: 16
Branch analysis from position: 6
Branch analysis from position: 13
filename:       /in/DWDvB
function name:  getObjectWithForeach
number of ops:  24
compiled vars:  !0 = $objectName, !1 = $objectArray, !2 = $iterations, !3 = $found, !4 = $count, !5 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  103     2        ASSIGN                                                   !2, 0
  104     3        ASSIGN                                                   !3, <false>
  105     4        ASSIGN                                                   !4, 0
  106     5      > JMP                                                      ->14
  107     6    >   FETCH_DIM_RW                                     $9      !1, !4
          7        PRE_INC_OBJ                                              $9, 'accessed'
  108     8        FETCH_DIM_R                                      ~11     !1, !4
          9        FETCH_OBJ_R                                      ~12     ~11, 'name'
         10        IS_IDENTICAL                                             !0, ~12
         11      > JMPZ                                                     ~13, ->13
  109    12    >   ASSIGN                                                   !3, <true>
  111    13    >   PRE_INC                                                  !4
  106    14    >   BOOL_NOT                                         ~16     !3
         15      > JMPNZ                                                    ~16, ->6
  113    16    > > FE_RESET_R                                       $17     !1, ->21
         17    > > FE_FETCH_R                                               $17, !5, ->21
  114    18    >   FETCH_OBJ_R                                      ~18     !5, 'accessed'
         19        ASSIGN_OP                                     1          !2, ~18
  113    20      > JMP                                                      ->17
         21    >   FE_FREE                                                  $17
  116    22      > RETURN                                                   !2
  117    23*     > RETURN                                                   null

End of function getobjectwithforeach

Function getobjectfromassociativearray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
Branch analysis from position: 9
filename:       /in/DWDvB
function name:  getObjectFromAssociativeArray
number of ops:  17
compiled vars:  !0 = $objectName, !1 = $objectArray, !2 = $iterations, !3 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  119     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  120     2        ASSIGN                                                   !2, 0
  121     3        FETCH_DIM_R                                      ~5      !1, !0
          4        FETCH_OBJ_R                                      ~6      ~5, 'name'
          5        IS_IDENTICAL                                             !0, ~6
          6      > JMPZ                                                     ~7, ->9
  122     7    >   FETCH_DIM_RW                                     $8      !1, !0
          8        PRE_INC_OBJ                                              $8, 'accessed'
  124     9    > > FE_RESET_R                                       $10     !1, ->14
         10    > > FE_FETCH_R                                               $10, !3, ->14
  125    11    >   FETCH_OBJ_R                                      ~11     !3, 'accessed'
         12        ASSIGN_OP                                     1          !2, ~11
  124    13      > JMP                                                      ->10
         14    >   FE_FREE                                                  $10
  127    15      > RETURN                                                   !2
  128    16*     > RETURN                                                   null

End of function getobjectfromassociativearray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.56 ms | 1420 KiB | 19 Q