3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @template TKey of array-key * @template TValue */ class Collection { /** * @var array<TKey, TValue> */ protected $items = []; /** * @param array<TKey, TValue> $items * @return void */ public function __construct($items = []) { $this->items = $items; } /** * @param TValue|array-key $key * @return bool */ public function containsStrict_foreach($key) { foreach ($this->items as $item) { if ($item === $key) { return true; } } return false; } /** * @param TValue|array-key $key * @return bool */ public function containsStrict_in_array($key) { return in_array($key, $this->items, true); } } /** * @template TValue * @param array<array-key, TValue> $items * @return array{int, int} */ function bench($items): array { $value = $items[count($items) / 2]; $collection = new Collection($items); $prev = hrtime(true); $collection->containsStrict_foreach($value); $foreach = hrtime(true) - $prev; $collection = new Collection($items); $prev = hrtime(true); $collection->containsStrict_in_array($value); $inArray = hrtime(true) - $prev; return [$foreach, $inArray]; } $fmt = "%5s | %-13.13s | %24s | %25s\n"; printf($fmt, 'n', 'name', 'containsStrict (foreach)', 'containsStrict (in_array)'); foreach ([100, 1000, 10000] as $n) { printf($fmt, $n, "serial int", ...bench(range(1, $n))); printf($fmt, $n, "random int", ...bench(array_map(fn () => random_int(1, $n), range(1, $n)))); printf($fmt, $n, "uniqid string", ...bench(array_map(fn () => uniqid(), range(1, $n)))); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 64
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 64
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
filename:       /in/UCqoe
function name:  (null)
number of ops:  66
compiled vars:  !0 = $fmt, !1 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   ASSIGN                                                   !0, '%255s+%7C+%25-13.13s+%7C+%2524s+%7C+%2525s%0A'
   70     1        INIT_FCALL                                               'printf'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'n'
          4        SEND_VAL                                                 'name'
          5        SEND_VAL                                                 'containsStrict+%28foreach%29'
          6        SEND_VAL                                                 'containsStrict+%28in_array%29'
          7        DO_ICALL                                                 
   71     8      > FE_RESET_R                                       $4      <array>, ->64
          9    > > FE_FETCH_R                                               $4, !1, ->64
   72    10    >   INIT_FCALL                                               'printf'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !1
         13        SEND_VAL                                                 'serial+int'
         14        INIT_FCALL                                               'bench'
         15        INIT_FCALL                                               'range'
         16        SEND_VAL                                                 1
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $5      
         19        SEND_VAR                                                 $5
         20        DO_FCALL                                      0  $6      
         21        SEND_UNPACK                                              $6
         22        CHECK_UNDEF_ARGS                                         
         23        DO_ICALL                                                 
   73    24        INIT_FCALL                                               'printf'
         25        SEND_VAR                                                 !0
         26        SEND_VAR                                                 !1
         27        SEND_VAL                                                 'random+int'
         28        INIT_FCALL                                               'bench'
         29        INIT_FCALL                                               'array_map'
         30        DECLARE_LAMBDA_FUNCTION                          ~8      [0]
         31        BIND_LEXICAL                                             ~8, !1
         32        SEND_VAL                                                 ~8
         33        INIT_FCALL                                               'range'
         34        SEND_VAL                                                 1
         35        SEND_VAR                                                 !1
         36        DO_ICALL                                         $9      
         37        SEND_VAR                                                 $9
         38        DO_ICALL                                         $10     
         39        SEND_VAR                                                 $10
         40        DO_FCALL                                      0  $11     
         41        SEND_UNPACK                                              $11
         42        CHECK_UNDEF_ARGS                                         
         43        DO_ICALL                                                 
   74    44        INIT_FCALL                                               'printf'
         45        SEND_VAR                                                 !0
         46        SEND_VAR                                                 !1
         47        SEND_VAL                                                 'uniqid+string'
         48        INIT_FCALL                                               'bench'
         49        INIT_FCALL                                               'array_map'
         50        DECLARE_LAMBDA_FUNCTION                          ~13     [1]
         51        SEND_VAL                                                 ~13
         52        INIT_FCALL                                               'range'
         53        SEND_VAL                                                 1
         54        SEND_VAR                                                 !1
         55        DO_ICALL                                         $14     
         56        SEND_VAR                                                 $14
         57        DO_ICALL                                         $15     
         58        SEND_VAR                                                 $15
         59        DO_FCALL                                      0  $16     
         60        SEND_UNPACK                                              $16
         61        CHECK_UNDEF_ARGS                                         
         62        DO_ICALL                                                 
   71    63      > JMP                                                      ->9
         64    >   FE_FREE                                                  $4
   75    65      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UCqoe
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   BIND_STATIC                                              !0
          1        INIT_FCALL                                               'random_int'
          2        SEND_VAL                                                 1
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5      > RETURN                                                   $1
          6*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UCqoe
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   INIT_FCALL                                               'uniqid'
          1        DO_ICALL                                         $0      
          2      > RETURN                                                   $0
          3*     > RETURN                                                   null

End of Dynamic Function 1

Function bench:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UCqoe
function name:  bench
number of ops:  43
compiled vars:  !0 = $items, !1 = $value, !2 = $collection, !3 = $prev, !4 = $foreach, !5 = $inArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   54     1        COUNT                                            ~6      !0
          2        DIV                                              ~7      ~6, 2
          3        FETCH_DIM_R                                      ~8      !0, ~7
          4        ASSIGN                                                   !1, ~8
   56     5        NEW                                              $10     'Collection'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !2, $10
   57     9        INIT_FCALL                                               'hrtime'
         10        SEND_VAL                                                 <true>
         11        DO_ICALL                                         $13     
         12        ASSIGN                                                   !3, $13
   58    13        INIT_METHOD_CALL                                         !2, 'containsStrict_foreach'
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0          
   59    16        INIT_FCALL                                               'hrtime'
         17        SEND_VAL                                                 <true>
         18        DO_ICALL                                         $16     
         19        SUB                                              ~17     $16, !3
         20        ASSIGN                                                   !4, ~17
   61    21        NEW                                              $19     'Collection'
         22        SEND_VAR_EX                                              !0
         23        DO_FCALL                                      0          
         24        ASSIGN                                                   !2, $19
   62    25        INIT_FCALL                                               'hrtime'
         26        SEND_VAL                                                 <true>
         27        DO_ICALL                                         $22     
         28        ASSIGN                                                   !3, $22
   63    29        INIT_METHOD_CALL                                         !2, 'containsStrict_in_array'
         30        SEND_VAR_EX                                              !1
         31        DO_FCALL                                      0          
   64    32        INIT_FCALL                                               'hrtime'
         33        SEND_VAL                                                 <true>
         34        DO_ICALL                                         $25     
         35        SUB                                              ~26     $25, !3
         36        ASSIGN                                                   !5, ~26
   66    37        INIT_ARRAY                                       ~28     !4
         38        ADD_ARRAY_ELEMENT                                ~28     !5
         39        VERIFY_RETURN_TYPE                                       ~28
         40      > RETURN                                                   ~28
   67    41*       VERIFY_RETURN_TYPE                                       
         42*     > RETURN                                                   null

End of function bench

Class Collection:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UCqoe
function name:  __construct
number of ops:  4
compiled vars:  !0 = $items
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV_INIT                                        !0      <array>
   20     1        ASSIGN_OBJ                                               'items'
          2        OP_DATA                                                  !0
   21     3      > RETURN                                                   null

End of function __construct

Function containsstrict_foreach:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/UCqoe
function name:  containsStrict_foreach
number of ops:  12
compiled vars:  !0 = $key, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        FETCH_OBJ_R                                      ~2      'items'
          2      > FE_RESET_R                                       $3      ~2, ->9
          3    > > FE_FETCH_R                                               $3, !1, ->9
   30     4    >   IS_IDENTICAL                                             !1, !0
          5      > JMPZ                                                     ~4, ->8
   31     6    >   FE_FREE                                                  $3
          7      > RETURN                                                   <true>
   29     8    > > JMP                                                      ->3
          9    >   FE_FREE                                                  $3
   34    10      > RETURN                                                   <false>
   35    11*     > RETURN                                                   null

End of function containsstrict_foreach

Function containsstrict_in_array:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UCqoe
function name:  containsStrict_in_array
number of ops:  9
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        INIT_FCALL                                               'in_array'
          2        SEND_VAR                                                 !0
          3        FETCH_OBJ_R                                      ~1      'items'
          4        SEND_VAL                                                 ~1
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $2      
          7      > RETURN                                                   $2
   44     8*     > RETURN                                                   null

End of function containsstrict_in_array

End of class Collection.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.76 ms | 1032 KiB | 23 Q