3v4l.org

run code in 500+ PHP versions simultaneously
<?php function randomPickByWeight(array $set) { $low=0; $high=0; $candidates=[]; foreach($set as $key=>$item) { $candidates[$high]=$key; $high+=$item["weight"]; } $pick=mt_rand($low,$high-1); while(!array_key_exists($pick,$candidates)) { $pick--; } return $set[$candidates[$pick]]; } $cache=[]; for($i=0;$i<100000;$i++) { $cache[]=["item"=>"item {$i}","weight"=>mt_rand(1,10)]; } $time=time(); for($i=0;$i<100;$i++) { print_r(randomPickByWeight($cache)); } $time=time()-$time; var_dump($time);
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 = 3
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 21
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 21
Branch analysis from position: 30
Branch analysis from position: 21
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 3
Branch analysis from position: 16
Branch analysis from position: 3
filename:       /in/jfJm6
function name:  (null)
number of ops:  38
compiled vars:  !0 = $cache, !1 = $i, !2 = $time
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   ASSIGN                                                       !0, <array>
   20     1        ASSIGN                                                       !1, 0
          2      > JMP                                                          ->14
   22     3    >   NOP                                                          
          4        FAST_CONCAT                                          ~6      'item+', !1
          5        INIT_ARRAY                                           ~7      ~6, 'item'
          6        INIT_FCALL                                                   'mt_rand'
          7        SEND_VAL                                                     1
          8        SEND_VAL                                                     10
          9        DO_ICALL                                             $8      
         10        ADD_ARRAY_ELEMENT                                    ~7      $8, 'weight'
         11        ASSIGN_DIM                                                   !0
         12        OP_DATA                                                      ~7
   20    13        PRE_INC                                                      !1
         14    >   IS_SMALLER                                                   !1, 100000
         15      > JMPNZ                                                        ~10, ->3
   24    16    >   INIT_FCALL                                                   'time'
         17        DO_ICALL                                             $11     
         18        ASSIGN                                                       !2, $11
   25    19        ASSIGN                                                       !1, 0
         20      > JMP                                                          ->28
   27    21    >   INIT_FCALL                                                   'print_r'
         22        INIT_FCALL                                                   'randompickbyweight'
         23        SEND_VAR                                                     !0
         24        DO_FCALL                                          0  $14     
         25        SEND_VAR                                                     $14
         26        DO_ICALL                                                     
   25    27        PRE_INC                                                      !1
         28    >   IS_SMALLER                                                   !1, 100
         29      > JMPNZ                                                        ~17, ->21
   29    30    >   INIT_FCALL                                                   'time'
         31        DO_ICALL                                             $18     
         32        SUB                                                  ~19     $18, !2
         33        ASSIGN                                                       !2, ~19
   30    34        INIT_FCALL                                                   'var_dump'
         35        SEND_VAR                                                     !2
         36        DO_ICALL                                                     
         37      > RETURN                                                       1

Function randompickbyweight:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 20
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 20
Branch analysis from position: 24
Branch analysis from position: 20
Branch analysis from position: 12
filename:       /in/jfJm6
function name:  randomPickByWeight
number of ops:  28
compiled vars:  !0 = $set, !1 = $low, !2 = $high, !3 = $candidates, !4 = $item, !5 = $key, !6 = $pick
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   RECV                                                 !0      
    4     1        ASSIGN                                                       !1, 0
    5     2        ASSIGN                                                       !2, 0
    6     3        ASSIGN                                                       !3, <array>
    7     4      > FE_RESET_R                                           $10     !0, ->12
          5    > > FE_FETCH_R                                           ~11     $10, !4, ->12
          6    >   ASSIGN                                                       !5, ~11
    9     7        ASSIGN_DIM                                                   !3, !2
          8        OP_DATA                                                      !5
   10     9        FETCH_DIM_R                                          ~14     !4, 'weight'
         10        ASSIGN_OP                                         1          !2, ~14
    7    11      > JMP                                                          ->5
         12    >   FE_FREE                                                      $10
   12    13        INIT_FCALL                                                   'mt_rand'
         14        SEND_VAR                                                     !1
         15        SUB                                                  ~16     !2, 1
         16        SEND_VAL                                                     ~16
         17        DO_ICALL                                             $17     
         18        ASSIGN                                                       !6, $17
   13    19      > JMP                                                          ->21
   15    20    >   PRE_DEC                                                      !6
   13    21    >   ARRAY_KEY_EXISTS                                     ~20     !6, !3
         22        BOOL_NOT                                             ~21     ~20
         23      > JMPNZ                                                        ~21, ->20
   17    24    >   FETCH_DIM_R                                          ~22     !3, !6
         25        FETCH_DIM_R                                          ~23     !0, ~22
         26      > RETURN                                                       ~23
   18    27*     > RETURN                                                       null

End of function randompickbyweight

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
310.22 ms | 1709 KiB | 19 Q