3v4l.org

run code in 300+ PHP versions simultaneously
<?php const N = 100_000; const TYPE = 0; function simple_alloc($id, $delete = false, $type = TYPE) { static $ids = []; if (is_int($id)) { $key = $id; } else { $key = array_search($id, $ids[$type] ?? [], true); } if ($delete) { if ($key !== false) { unset($ids[$type][$key]); } return $key; } if ($key === false) { $key = count($ids[$type] ?? []); $ids[$type][$key] = $id; } return $key; } function freelist_alloc($id, $delete = false, $type = TYPE) { static $ids = []; static $freelist = []; $freelist[$type] ??= []; $ids[$type] ??= []; if (is_int($id)) { if ($delete) $freelist[$type][] = $id; return $id; } $match = null; foreach ($ids[$type] as $i => $arr) { if (in_array($i, $freelist[$type], true)) continue; if ($arr === $id) { $match = $i; if ($delete) $freelist[$type][] = $i; break; } } if ($match === null) { $nextId = array_pop($freelist[$type]); if ($nextId === null) { $nextId = count($ids[$type]); $ids[$type][] = $id; } else { $ids[$type][$nextId] = $id; } return $nextId; } return $match; } $t1 = microtime(true); $live1 = []; for ($i = 0; $i < N; $i++) { if ($live1 && mt_rand(0, 1)) { $k = array_rand($live1); simple_alloc($live1[$k], true); unset($live1[$k]); } else { $obj = 'obj-' . $i . '-' . mt_rand(); $id = simple_alloc($obj); $live1[] = $id; } } $dt_simple = microtime(true) - $t1; $t2 = microtime(true); $live2 = []; for ($i = 0; $i < N; $i++) { if ($live2 && mt_rand(0, 1)) { $k = array_rand($live2); freelist_alloc($live2[$k], true); unset($live2[$k]); } else { $obj = 'obj-' . $i . '-' . mt_rand(); $id = freelist_alloc($obj); $live2[] = $id; } } $dt_freelist = microtime(true) - $t2; // --- Results --- echo "Random Allocation/Free Benchmark (N = " . number_format(N) . ")\n"; echo "Simple allocator: " . number_format($dt_simple, 4) . " sec\n"; echo "Freelist allocator: " . number_format($dt_freelist, 4) . " sec\n"; if ($dt_freelist > 0) { echo "Simple is " . number_format($dt_freelist / $dt_simple, 2) . "x faster\n"; } else { echo "\n"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 9
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 55
Branch analysis from position: 89
2 jumps found. (Code = 43) Position 1 = 117, Position 2 = 126
Branch analysis from position: 117
1 jumps found. (Code = 42) Position 1 = 127
Branch analysis from position: 127
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 126
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 55
2 jumps found. (Code = 46) Position 1 = 56, Position 2 = 61
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 73
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 55
Branch analysis from position: 89
Branch analysis from position: 55
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 55
Branch analysis from position: 89
Branch analysis from position: 55
Branch analysis from position: 61
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 27
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 9
Branch analysis from position: 43
Branch analysis from position: 9
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 9
Branch analysis from position: 43
Branch analysis from position: 9
Branch analysis from position: 15
filename:       /in/1h0jU
function name:  (null)
number of ops:  128
compiled vars:  !0 = $t1, !1 = $live1, !2 = $i, !3 = $k, !4 = $obj, !5 = $id, !6 = $dt_simple, !7 = $t2, !8 = $live2, !9 = $dt_freelist
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CONST                                            'N', 100000
    4     1        DECLARE_CONST                                            'TYPE', 0
   62     2        INIT_FCALL                                               'microtime'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $10     
          5        ASSIGN                                                   !0, $10
   63     6        ASSIGN                                                   !1, <array>
   64     7        ASSIGN                                                   !2, 0
          8      > JMP                                                      ->40
   65     9    > > JMPZ_EX                                          ~14     !1, ->15
         10    >   INIT_FCALL                                               'mt_rand'
         11        SEND_VAL                                                 0
         12        SEND_VAL                                                 1
         13        DO_ICALL                                         $15     
         14        BOOL                                             ~14     $15
         15    > > JMPZ                                                     ~14, ->27
   66    16    >   INIT_FCALL                                               'array_rand'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $16     
         19        ASSIGN                                                   !3, $16
   67    20        INIT_FCALL                                               'simple_alloc'
         21        FETCH_DIM_R                                      ~18     !1, !3
         22        SEND_VAL                                                 ~18
         23        SEND_VAL                                                 <true>
         24        DO_FCALL                                      0          
   68    25        UNSET_DIM                                                !1, !3
   65    26      > JMP                                                      ->39
   70    27    >   CONCAT                                           ~20     'obj-', !2
         28        CONCAT                                           ~21     ~20, '-'
         29        INIT_FCALL                                               'mt_rand'
         30        DO_ICALL                                         $22     
         31        CONCAT                                           ~23     ~21, $22
         32        ASSIGN                                                   !4, ~23
   71    33        INIT_FCALL                                               'simple_alloc'
         34        SEND_VAR                                                 !4
         35        DO_FCALL                                      0  $25     
         36        ASSIGN                                                   !5, $25
   72    37        ASSIGN_DIM                                               !1
         38        OP_DATA                                                  !5
   64    39    >   PRE_INC                                                  !2
         40    >   FETCH_CONSTANT                                   ~29     'N'
         41        IS_SMALLER                                               !2, ~29
         42      > JMPNZ                                                    ~30, ->9
   75    43    >   INIT_FCALL                                               'microtime'
         44        SEND_VAL                                                 <true>
         45        DO_ICALL                                         $31     
         46        SUB                                              ~32     $31, !0
         47        ASSIGN                                                   !6, ~32
   77    48        INIT_FCALL                                               'microtime'
         49        SEND_VAL                                                 <true>
         50        DO_ICALL                                         $34     
         51        ASSIGN                                                   !7, $34
   78    52        ASSIGN                                                   !8, <array>
   79    53        ASSIGN                                                   !2, 0
         54      > JMP                                                      ->86
   80    55    > > JMPZ_EX                                          ~38     !8, ->61
         56    >   INIT_FCALL                                               'mt_rand'
         57        SEND_VAL                                                 0
         58        SEND_VAL                                                 1
         59        DO_ICALL                                         $39     
         60        BOOL                                             ~38     $39
         61    > > JMPZ                                                     ~38, ->73
   81    62    >   INIT_FCALL                                               'array_rand'
         63        SEND_VAR                                                 !8
         64        DO_ICALL                                         $40     
         65        ASSIGN                                                   !3, $40
   82    66        INIT_FCALL                                               'freelist_alloc'
         67        FETCH_DIM_R                                      ~42     !8, !3
         68        SEND_VAL                                                 ~42
         69        SEND_VAL                                                 <true>
         70        DO_FCALL                                      0          
   83    71        UNSET_DIM                                                !8, !3
   80    72      > JMP                                                      ->85
   85    73    >   CONCAT                                           ~44     'obj-', !2
         74        CONCAT                                           ~45     ~44, '-'
         75        INIT_FCALL                                               'mt_rand'
         76        DO_ICALL                                         $46     
         77        CONCAT                                           ~47     ~45, $46
         78        ASSIGN                                                   !4, ~47
   86    79        INIT_FCALL                                               'freelist_alloc'
         80        SEND_VAR                                                 !4
         81        DO_FCALL                                      0  $49     
         82        ASSIGN                                                   !5, $49
   87    83        ASSIGN_DIM                                               !8
         84        OP_DATA                                                  !5
   79    85    >   PRE_INC                                                  !2
         86    >   FETCH_CONSTANT                                   ~53     'N'
         87        IS_SMALLER                                               !2, ~53
         88      > JMPNZ                                                    ~54, ->55
   90    89    >   INIT_FCALL                                               'microtime'
         90        SEND_VAL                                                 <true>
         91        DO_ICALL                                         $55     
         92        SUB                                              ~56     $55, !7
         93        ASSIGN                                                   !9, ~56
   93    94        INIT_FCALL                                               'number_format'
         95        FETCH_CONSTANT                                   ~58     'N'
         96        SEND_VAL                                                 ~58
         97        DO_ICALL                                         $59     
         98        CONCAT                                           ~60     'Random+Allocation%2FFree+Benchmark+%28N+%3D+', $59
         99        CONCAT                                           ~61     ~60, '%29%0A'
        100        ECHO                                                     ~61
   94   101        INIT_FCALL                                               'number_format'
        102        SEND_VAR                                                 !6
        103        SEND_VAL                                                 4
        104        DO_ICALL                                         $62     
        105        CONCAT                                           ~63     'Simple+allocator%3A+++++', $62
        106        CONCAT                                           ~64     ~63, '+sec%0A'
        107        ECHO                                                     ~64
   95   108        INIT_FCALL                                               'number_format'
        109        SEND_VAR                                                 !9
        110        SEND_VAL                                                 4
        111        DO_ICALL                                         $65     
        112        CONCAT                                           ~66     'Freelist+allocator%3A+++', $65
        113        CONCAT                                           ~67     ~66, '+sec%0A'
        114        ECHO                                                     ~67
   96   115        IS_SMALLER                                               0, !9
        116      > JMPZ                                                     ~68, ->126
   97   117    >   INIT_FCALL                                               'number_format'
        118        DIV                                              ~69     !9, !6
        119        SEND_VAL                                                 ~69
        120        SEND_VAL                                                 2
        121        DO_ICALL                                         $70     
        122        CONCAT                                           ~71     'Simple+is+', $70
        123        CONCAT                                           ~72     ~71, 'x+faster%0A'
        124        ECHO                                                     ~72
   96   125      > JMP                                                      ->127
   99   126    >   ECHO                                                     '%0A'
  100   127    > > RETURN                                                   1

Function simple_alloc:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 33
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
Branch analysis from position: 23
filename:       /in/1h0jU
function name:  simple_alloc
number of ops:  35
compiled vars:  !0 = $id, !1 = $delete, !2 = $type, !3 = $ids, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
          2        RECV_INIT                                        !2      <const ast>
    8     3        BIND_STATIC                                              !3
   10     4        TYPE_CHECK                                   16          !0
          5      > JMPZ                                                     ~5, ->8
   11     6    >   ASSIGN                                                   !4, !0
   10     7      > JMP                                                      ->17
   13     8    >   INIT_FCALL                                               'array_search'
          9        SEND_VAR                                                 !0
         10        FETCH_DIM_IS                                     ~7      !3, !2
         11        COALESCE                                         ~8      ~7
         12        QM_ASSIGN                                        ~8      <array>
         13        SEND_VAL                                                 ~8
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $9      
         16        ASSIGN                                                   !4, $9
   16    17    > > JMPZ                                                     !1, ->23
   17    18    >   TYPE_CHECK                                  1018          !4
         19      > JMPZ                                                     ~11, ->22
   18    20    >   FETCH_DIM_UNSET                                  $12     !3, !2
         21        UNSET_DIM                                                $12, !4
   20    22    > > RETURN                                                   !4
   23    23    >   TYPE_CHECK                                    4          !4
         24      > JMPZ                                                     ~13, ->33
   24    25    >   FETCH_DIM_IS                                     ~14     !3, !2
         26        COALESCE                                         ~15     ~14
         27        QM_ASSIGN                                        ~15     <array>
         28        COUNT                                            ~16     ~15
         29        ASSIGN                                                   !4, ~16
   25    30        FETCH_DIM_W                                      $18     !3, !2
         31        ASSIGN_DIM                                               $18, !4
         32        OP_DATA                                                  !0
   27    33    > > RETURN                                                   !4
   28    34*     > RETURN                                                   null

End of function simple_alloc

Function freelist_alloc:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 24
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 23
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
Branch analysis from position: 24
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 46
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 46
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 37
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 45
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 44
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 67
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 63
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 46
Branch analysis from position: 46
filename:       /in/1h0jU
function name:  freelist_alloc
number of ops:  69
compiled vars:  !0 = $id, !1 = $delete, !2 = $type, !3 = $ids, !4 = $freelist, !5 = $match, !6 = $arr, !7 = $i, !8 = $nextId
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
          2        RECV_INIT                                        !2      <const ast>
   32     3        BIND_STATIC                                              !3
   33     4        BIND_STATIC                                              !4
   34     5        FETCH_DIM_IS                                     ~9      !4, !2
          6        COALESCE                                         ~10     ~9
          7        ASSIGN_DIM                                       ~11     !4, !2
          8        OP_DATA                                                  <array>
          9        QM_ASSIGN                                        ~10     ~11
         10        FREE                                                     ~10
   35    11        FETCH_DIM_IS                                     ~12     !3, !2
         12        COALESCE                                         ~13     ~12
         13        ASSIGN_DIM                                       ~14     !3, !2
         14        OP_DATA                                                  <array>
         15        QM_ASSIGN                                        ~13     ~14
         16        FREE                                                     ~13
   36    17        TYPE_CHECK                                   16          !0
         18      > JMPZ                                                     ~15, ->24
   37    19    > > JMPZ                                                     !1, ->23
         20    >   FETCH_DIM_W                                      $16     !4, !2
         21        ASSIGN_DIM                                               $16
         22        OP_DATA                                                  !0
   38    23    > > RETURN                                                   !0
   40    24    >   ASSIGN                                                   !5, null
   41    25        FETCH_DIM_R                                      ~19     !3, !2
         26      > FE_RESET_R                                       $20     ~19, ->46
         27    > > FE_FETCH_R                                       ~21     $20, !6, ->46
         28    >   ASSIGN                                                   !7, ~21
   42    29        INIT_FCALL                                               'in_array'
         30        SEND_VAR                                                 !7
         31        FETCH_DIM_R                                      ~23     !4, !2
         32        SEND_VAL                                                 ~23
         33        SEND_VAL                                                 <true>
         34        DO_ICALL                                         $24     
         35      > JMPZ                                                     $24, ->37
         36    > > JMP                                                      ->27
   43    37    >   IS_IDENTICAL                                             !6, !0
         38      > JMPZ                                                     ~25, ->45
   44    39    >   ASSIGN                                                   !5, !7
   45    40      > JMPZ                                                     !1, ->44
         41    >   FETCH_DIM_W                                      $27     !4, !2
         42        ASSIGN_DIM                                               $27
         43        OP_DATA                                                  !7
   46    44    > > JMP                                                      ->46
   41    45    > > JMP                                                      ->27
         46    >   FE_FREE                                                  $20
   49    47        TYPE_CHECK                                    2          !5
         48      > JMPZ                                                     ~29, ->67
   50    49    >   INIT_FCALL                                               'array_pop'
         50        FETCH_DIM_W                                      $30     !4, !2
         51        SEND_REF                                                 $30
         52        DO_ICALL                                         $31     
         53        ASSIGN                                                   !8, $31
   51    54        TYPE_CHECK                                    2          !8
         55      > JMPZ                                                     ~33, ->63
   52    56    >   FETCH_DIM_R                                      ~34     !3, !2
         57        COUNT                                            ~35     ~34
         58        ASSIGN                                                   !8, ~35
   53    59        FETCH_DIM_W                                      $37     !3, !2
         60        ASSIGN_DIM                                               $37
         61        OP_DATA                                                  !0
   51    62      > JMP                                                      ->66
   55    63    >   FETCH_DIM_W                                      $39     !3, !2
         64        ASSIGN_DIM                                               $39, !8
         65        OP_DATA                                                  !0
   57    66    > > RETURN                                                   !8
   59    67    > > RETURN                                                   !5
   60    68*     > RETURN                                                   null

End of function freelist_alloc

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.29 ms | 1032 KiB | 24 Q