3v4l.org

run code in 300+ PHP versions simultaneously
<?php $iter = 0; function weight($array) { global $iter; $sum = array_sum($array); $rnd = mt_rand(0,$sum); foreach($array as $k=>$v) { $iter++; if($rnd <= $v) { return $k; } $rnd = $rnd - $v; } } $weights = range(0, 100000); $values = array_fill(0, count($weights), 0); for ($x = 0; $x < 1000; $x++) { $w = weight($weights); $values[$w] = $values[$w] + 1; } echo "$iter iterations performed"; var_dump($values);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 15
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 15
Branch analysis from position: 26
Branch analysis from position: 15
filename:       /in/camlS
function name:  (null)
number of ops:  33
compiled vars:  !0 = $iter, !1 = $weights, !2 = $values, !3 = $x, !4 = $w
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 0
   20     1        INIT_FCALL                                               'range'
          2        SEND_VAL                                                 0
          3        SEND_VAL                                                 100000
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !1, $6
   21     6        INIT_FCALL                                               'array_fill'
          7        SEND_VAL                                                 0
          8        COUNT                                            ~8      !1
          9        SEND_VAL                                                 ~8
         10        SEND_VAL                                                 0
         11        DO_ICALL                                         $9      
         12        ASSIGN                                                   !2, $9
   23    13        ASSIGN                                                   !3, 0
         14      > JMP                                                      ->24
   25    15    >   INIT_FCALL                                               'weight'
         16        SEND_VAR                                                 !1
         17        DO_FCALL                                      0  $12     
         18        ASSIGN                                                   !4, $12
   26    19        FETCH_DIM_R                                      ~15     !2, !4
         20        ADD                                              ~16     ~15, 1
         21        ASSIGN_DIM                                               !2, !4
         22        OP_DATA                                                  ~16
   23    23        PRE_INC                                                  !3
         24    >   IS_SMALLER                                               !3, 1000
         25      > JMPNZ                                                    ~18, ->15
   29    26    >   NOP                                                      
         27        FAST_CONCAT                                      ~19     !0, '+iterations+performed'
         28        ECHO                                                     ~19
   31    29        INIT_FCALL                                               'var_dump'
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Function weight:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 22
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 22
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/camlS
function name:  weight
number of ops:  24
compiled vars:  !0 = $array, !1 = $iter, !2 = $sum, !3 = $rnd, !4 = $v, !5 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        BIND_GLOBAL                                              !1, 'iter'
    6     2        INIT_FCALL                                               'array_sum'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !2, $6
    7     6        INIT_FCALL                                               'mt_rand'
          7        SEND_VAL                                                 0
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $8      
         10        ASSIGN                                                   !3, $8
    8    11      > FE_RESET_R                                       $10     !0, ->22
         12    > > FE_FETCH_R                                       ~11     $10, !4, ->22
         13    >   ASSIGN                                                   !5, ~11
   10    14        PRE_INC                                                  !1
   11    15        IS_SMALLER_OR_EQUAL                                      !3, !4
         16      > JMPZ                                                     ~14, ->19
   13    17    >   FE_FREE                                                  $10
         18      > RETURN                                                   !5
   15    19    >   SUB                                              ~15     !3, !4
         20        ASSIGN                                                   !3, ~15
    8    21      > JMP                                                      ->12
         22    >   FE_FREE                                                  $10
   17    23      > RETURN                                                   null

End of function weight

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.87 ms | 1394 KiB | 24 Q