3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * 与えられたアイテムの累積重みを計算します。 * * @param array<int|float> $items アイテムとその重みを格納した配列。キーにアイテム、値に重み * @return array{0: array<int|float>, 1: int|float} 累積重みの配列と総重み。累積重みの配列はキーにアイテム、値に累積重み */ function prepare_cumulative_weights(array $items): array { // 累積重みの配列を初期化 $cumulative_weights = []; // 重みの総和を初期化 $total = 0.0; // 各アイテムに対して累積重みを計算 foreach ($items as $item => $weight) { $total += $weight; $cumulative_weights[$item] = $total; } // 累積重みの配列と総重みを返す return [$cumulative_weights, $total]; } /** * 累積重みを基にランダムにアイテムを選択します。 * * @param array<int|float> $cumulative_weights 累積重みの配列 * @param float|int $total 総重み * @param string $algo アルゴリズム名。binary_search, linear_search * @return float|int|string 選択されたアイテムのキー */ function weighted_random_pick(array $cumulative_weights, float|int $total, string $algo = 'binary_search'): float|int|string { // ランダムな数を生成 $num = mt_rand() / mt_getrandmax() * $total; // 生成された数に基づいてアイテムを探索して選択 return $algo($cumulative_weights, $num); } // 何度も参照する値のキャッシュ置き場 class WeightRandomCacheStorage { public static array|null $arrayValues; public static array|null $arrayKeys; public static function clear(): void { self::$arrayValues = null; self::$arrayKeys = null; } } /** * 二分探索を行い、指定した値以上の最初の要素を見つけます。 * * @param array<float|int> $array 検索対象の配列。昇順で累積重みが値に入っている値 * @param float|int|string $value 検索する値 * @return float|int|string 指定した値以上の最初の要素 */ function binary_search(array $array, float|int|string $value): float|int|string { WeightRandomCacheStorage::$arrayValues ??= array_values($array); WeightRandomCacheStorage::$arrayKeys ??= array_keys($array); $left = 0; $right = count(WeightRandomCacheStorage::$arrayValues) - 1; // 二分探索 while ($left <= $right) { $middle = floor(($left + $right) / 2); if (WeightRandomCacheStorage::$arrayValues[$middle] == $value) { return $middle; } if (WeightRandomCacheStorage::$arrayValues[$middle] < $value) { $left = $middle + 1; } else { $right = $middle - 1; } } // $valueを超える最初の要素の値を返す return WeightRandomCacheStorage::$arrayKeys[$left]; } /** * 線形探索を行い、指定した値以上の最初の要素を見つけます。 * * @param array<float|int> $array 検索対象の配列。昇順で累積重みが値に入っている値 * @param float|int|string $value 検索する値 * @return float|int|string 指定した値以上の最初の要素 */ function linear_search(array $array, float|int|string $value): float|int|string { global $arrayV, $arrayK; $arrayV ??= array_values($array); $arrayK ??= array_keys($array); // 線形探索 for ($i = 0; $i < count($arrayV); ++$i) { if ($arrayV[$i] >= $value) { return $arrayK[$i]; // $value以上の最初の要素のキーを返す } } return $arrayK[$i]; // 指定された値以上の要素が見つからない場合 } /** * 与えられた回数だけアイテムをランダムに選択します。 * * @param array<int|float> $items アイテムとその重みを格納した配列。キーにアイテム、値に重み * @param int $count ランダムに選択する回数 * @param string|null $algo アルゴリズム名。binary_search, linear_search * @return array<int> 選択された各アイテムの数を格納した配列。キーにアイテム、値に選択された回数 */ function some_weighted_random_pick(array $items, int $count, string|null $algo = null): array { if(!isset($algo)){ // 要素が少ないなら線形探索、多いなら二分探索 $algo = count($items) < 50 ? 'linear_search' : 'binary_search'; } // 累積重みと総重みを計算 list($cumulative_weights, $total) = prepare_cumulative_weights($items); // 結果を格納する配列を初期化 $results = array_combine(array_keys($items), array_fill(0, count($items), 0)); // 指定された回数だけランダムにアイテムを選択 for ($i = 0; $i < $count; ++$i) { $result = weighted_random_pick($cumulative_weights, $total, $algo); ++$results[$result]; } WeightRandomCacheStorage::clear(); // 選択結果を返す return $results; } /** * 一度だけアイテムをランダムに選択します。 * * @param array<int|float> $items アイテムとその重みを格納した配列。キーにアイテム、値に重み * @param string $algo アルゴリズム名。binary_search, linear_search * @return float|int|string 選択されたアイテムのキー */ function single_weighted_random_pick(array $items, string $algo = 'binary_search'): float|int|string { // 累積重みと総重みを計算 list($cumulative_weights, $total) = prepare_cumulative_weights($items); // ランダムにアイテムを選択 $ret = weighted_random_pick($cumulative_weights, $total, $algo); WeightRandomCacheStorage::clear(); return $ret; } /** 使用例 **/ // ランダム対象 $items = []; // どれが選ばれたのか集計する配列 $results = []; for ($i = 0; $i < 10; ++$i) { $items["item$i"] = [ 'name' => "item$i", 'weight' => random_int(1, 1e5), ]; $results[$i] = 0; } // アイテムをシャッフル shuffle($items); // 実行するランダム選択回数 $num_trials = 1e5; foreach([null, 'linear_search', 'binary_search',] as $algo) { // 処理開始時間を記録 & ランダムなアイテムの選択を繰り返す $start = microtime(true); $results = any_item_some_weighted_random_pick($items, $num_trials, static fn($item)=>$item['weight'], $algo); $end = microtime(true); // 処理時間を表示 $algo ??= 'auto_algo_select'; echo $algo . ' Time: ' . ($end - $start) . "\n"; // 選択結果を表示 foreach($results as $item => $count) { echo "key:\t$item\tweight:\t{$items[$item]['weight']}\trate:\t" . ($count / $num_trials) . "\n"; } } function any_item_some_weighted_random_pick(array $items, int $count, callable|null $weightGetter = null, string|null $algo = null) { $_items = []; if(isset($weightGetter)) { foreach($items as $key => $item) { $_items[$key] = $weightGetter($item); } } else { $_items = $items; } return some_weighted_random_pick($_items, $count, $algo); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 4
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 69
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 69
Branch analysis from position: 27
2 jumps found. (Code = 77) Position 1 = 53, Position 2 = 67
Branch analysis from position: 53
2 jumps found. (Code = 78) Position 1 = 54, Position 2 = 67
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 67
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 69
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 4
Branch analysis from position: 21
Branch analysis from position: 4
filename:       /in/PLjcH
function name:  (null)
number of ops:  71
compiled vars:  !0 = $items, !1 = $results, !2 = $i, !3 = $num_trials, !4 = $algo, !5 = $start, !6 = $end, !7 = $count, !8 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  162     0  E >   ASSIGN                                                   !0, <array>
  164     1        ASSIGN                                                   !1, <array>
  165     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->19
  166     4    >   NOP                                                      
          5        FAST_CONCAT                                      ~12     'item', !2
  167     6        NOP                                                      
          7        FAST_CONCAT                                      ~14     'item', !2
          8        INIT_ARRAY                                       ~15     ~14, 'name'
  168     9        INIT_FCALL                                               'random_int'
         10        SEND_VAL                                                 1
         11        SEND_VAL                                                 100000
         12        DO_ICALL                                         $16     
         13        ADD_ARRAY_ELEMENT                                ~15     $16, 'weight'
  166    14        ASSIGN_DIM                                               !0, ~12
  168    15        OP_DATA                                                  ~15
  170    16        ASSIGN_DIM                                               !1, !2
         17        OP_DATA                                                  0
  165    18        PRE_INC                                                  !2
         19    >   IS_SMALLER                                               !2, 10
         20      > JMPNZ                                                    ~19, ->4
  173    21    >   INIT_FCALL                                               'shuffle'
         22        SEND_REF                                                 !0
         23        DO_ICALL                                                 
  175    24        ASSIGN                                                   !3, 100000
  176    25      > FE_RESET_R                                       $22     <array>, ->69
         26    > > FE_FETCH_R                                               $22, !4, ->69
  178    27    >   INIT_FCALL                                               'microtime'
         28        SEND_VAL                                                 <true>
         29        DO_ICALL                                         $23     
         30        ASSIGN                                                   !5, $23
  179    31        INIT_FCALL_BY_NAME                                       'any_item_some_weighted_random_pick'
         32        SEND_VAR_EX                                              !0
         33        SEND_VAR_EX                                              !3
         34        DECLARE_LAMBDA_FUNCTION                          ~25     [0]
         35        SEND_VAL_EX                                              ~25
         36        SEND_VAR_EX                                              !4
         37        DO_FCALL                                      0  $26     
         38        ASSIGN                                                   !1, $26
  180    39        INIT_FCALL                                               'microtime'
         40        SEND_VAL                                                 <true>
         41        DO_ICALL                                         $28     
         42        ASSIGN                                                   !6, $28
  183    43        COALESCE                                         ~30     !4
         44        ASSIGN                                           ~31     !4, 'auto_algo_select'
         45        QM_ASSIGN                                        ~30     ~31
         46        FREE                                                     ~30
  184    47        CONCAT                                           ~32     !4, '+Time%3A+'
         48        SUB                                              ~33     !6, !5
         49        CONCAT                                           ~34     ~32, ~33
         50        CONCAT                                           ~35     ~34, '%0A'
         51        ECHO                                                     ~35
  186    52      > FE_RESET_R                                       $36     !1, ->67
         53    > > FE_FETCH_R                                       ~37     $36, !7, ->67
         54    >   ASSIGN                                                   !8, ~37
  187    55        ROPE_INIT                                     5  ~42     'key%3A%09'
         56        ROPE_ADD                                      1  ~42     ~42, !8
         57        ROPE_ADD                                      2  ~42     ~42, '%09weight%3A%09'
         58        FETCH_DIM_R                                      ~39     !0, !8
         59        FETCH_DIM_R                                      ~40     ~39, 'weight'
         60        ROPE_ADD                                      3  ~42     ~42, ~40
         61        ROPE_END                                      4  ~41     ~42, '%09rate%3A%09'
         62        DIV                                              ~45     !7, !3
         63        CONCAT                                           ~46     ~41, ~45
         64        CONCAT                                           ~47     ~46, '%0A'
         65        ECHO                                                     ~47
  186    66      > JMP                                                      ->53
         67    >   FE_FREE                                                  $36
  176    68      > JMP                                                      ->26
         69    >   FE_FREE                                                  $22
  204    70      > 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/PLjcH
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  179     0  E >   RECV                                             !0      
          1        FETCH_DIM_R                                      ~1      !0, 'weight'
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of Dynamic Function 0

Function prepare_cumulative_weights:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/PLjcH
function name:  prepare_cumulative_weights
number of ops:  17
compiled vars:  !0 = $items, !1 = $cumulative_weights, !2 = $total, !3 = $weight, !4 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   12     1        ASSIGN                                                   !1, <array>
   15     2        ASSIGN                                                   !2, 0
   18     3      > FE_RESET_R                                       $7      !0, ->10
          4    > > FE_FETCH_R                                       ~8      $7, !3, ->10
          5    >   ASSIGN                                                   !4, ~8
   19     6        ASSIGN_OP                                     1          !2, !3
   20     7        ASSIGN_DIM                                               !1, !4
          8        OP_DATA                                                  !2
   18     9      > JMP                                                      ->4
         10    >   FE_FREE                                                  $7
   24    11        INIT_ARRAY                                       ~12     !1
         12        ADD_ARRAY_ELEMENT                                ~12     !2
         13        VERIFY_RETURN_TYPE                                       ~12
         14      > RETURN                                                   ~12
   25    15*       VERIFY_RETURN_TYPE                                       
         16*     > RETURN                                                   null

End of function prepare_cumulative_weights

Function weighted_random_pick:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PLjcH
function name:  weighted_random_pick
number of ops:  18
compiled vars:  !0 = $cumulative_weights, !1 = $total, !2 = $algo, !3 = $num
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      'binary_search'
   38     3        INIT_FCALL                                               'mt_rand'
          4        DO_ICALL                                         $4      
          5        INIT_FCALL                                               'mt_getrandmax'
          6        DO_ICALL                                         $5      
          7        DIV                                              ~6      $4, $5
          8        MUL                                              ~7      !1, ~6
          9        ASSIGN                                                   !3, ~7
   41    10        INIT_DYNAMIC_CALL                                        !2
         11        SEND_VAR_EX                                              !0
         12        SEND_VAR_EX                                              !3
         13        DO_FCALL                                      0  $9      
         14        VERIFY_RETURN_TYPE                                       $9
         15      > RETURN                                                   $9
   42    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function weighted_random_pick

Function binary_search:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 26
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 38
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 45
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 26
Branch analysis from position: 49
Branch analysis from position: 26
filename:       /in/PLjcH
function name:  binary_search
number of ops:  55
compiled vars:  !0 = $array, !1 = $value, !2 = $left, !3 = $right, !4 = $middle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   65     2        FETCH_STATIC_PROP_IS                             ~5      'arrayValues', 'WeightRandomCacheStorage'
          3        COALESCE                                         ~6      ~5
          4        INIT_FCALL                                               'array_values'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $7      
          7        ASSIGN_STATIC_PROP                               ~8      'arrayValues', 'WeightRandomCacheStorage'
          8        OP_DATA                                                  $7
          9        QM_ASSIGN                                        ~6      ~8
         10        FREE                                                     ~6
   66    11        FETCH_STATIC_PROP_IS                             ~9      'arrayKeys', 'WeightRandomCacheStorage'
         12        COALESCE                                         ~10     ~9
         13        INIT_FCALL                                               'array_keys'
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $11     
         16        ASSIGN_STATIC_PROP                               ~12     'arrayKeys', 'WeightRandomCacheStorage'
         17        OP_DATA                                                  $11
         18        QM_ASSIGN                                        ~10     ~12
         19        FREE                                                     ~10
   67    20        ASSIGN                                                   !2, 0
   68    21        FETCH_STATIC_PROP_R          unknown             ~14     'arrayValues'
         22        COUNT                                            ~15     ~14
         23        SUB                                              ~16     ~15, 1
         24        ASSIGN                                                   !3, ~16
   71    25      > JMP                                                      ->47
   72    26    >   INIT_FCALL                                               'floor'
         27        ADD                                              ~18     !2, !3
         28        DIV                                              ~19     ~18, 2
         29        SEND_VAL                                                 ~19
         30        DO_ICALL                                         $20     
         31        ASSIGN                                                   !4, $20
   74    32        FETCH_STATIC_PROP_R          unknown             ~22     'arrayValues'
         33        FETCH_DIM_R                                      ~23     ~22, !4
         34        IS_EQUAL                                                 !1, ~23
         35      > JMPZ                                                     ~24, ->38
   75    36    >   VERIFY_RETURN_TYPE                                       !4
         37      > RETURN                                                   !4
   78    38    >   FETCH_STATIC_PROP_R          unknown             ~25     'arrayValues'
         39        FETCH_DIM_R                                      ~26     ~25, !4
         40        IS_SMALLER                                               ~26, !1
         41      > JMPZ                                                     ~27, ->45
   79    42    >   ADD                                              ~28     !4, 1
         43        ASSIGN                                                   !2, ~28
   78    44      > JMP                                                      ->47
   81    45    >   SUB                                              ~30     !4, 1
         46        ASSIGN                                                   !3, ~30
   71    47    >   IS_SMALLER_OR_EQUAL                                      !2, !3
         48      > JMPNZ                                                    ~32, ->26
   86    49    >   FETCH_STATIC_PROP_R          unknown             ~33     'arrayKeys'
         50        FETCH_DIM_R                                      ~34     ~33, !2
         51        VERIFY_RETURN_TYPE                                       ~34
         52      > RETURN                                                   ~34
   87    53*       VERIFY_RETURN_TYPE                                       
         54*     > RETURN                                                   null

End of function binary_search

Function linear_search:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 20
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 26
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 20
Branch analysis from position: 30
Branch analysis from position: 20
filename:       /in/PLjcH
function name:  linear_search
number of ops:  35
compiled vars:  !0 = $array, !1 = $value, !2 = $arrayV, !3 = $arrayK, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   96     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   98     2        BIND_GLOBAL                                              !2, 'arrayV'
          3        BIND_GLOBAL                                              !3, 'arrayK'
   99     4        COALESCE                                         ~5      !2
          5        INIT_FCALL                                               'array_values'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $6      
          8        ASSIGN                                           ~7      !2, $6
          9        QM_ASSIGN                                        ~5      ~7
         10        FREE                                                     ~5
  100    11        COALESCE                                         ~8      !3
         12        INIT_FCALL                                               'array_keys'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $9      
         15        ASSIGN                                           ~10     !3, $9
         16        QM_ASSIGN                                        ~8      ~10
         17        FREE                                                     ~8
  103    18        ASSIGN                                                   !4, 0
         19      > JMP                                                      ->27
  104    20    >   FETCH_DIM_R                                      ~12     !2, !4
         21        IS_SMALLER_OR_EQUAL                                      !1, ~12
         22      > JMPZ                                                     ~13, ->26
  105    23    >   FETCH_DIM_R                                      ~14     !3, !4
         24        VERIFY_RETURN_TYPE                                       ~14
         25      > RETURN                                                   ~14
  103    26    >   PRE_INC                                                  !4
         27    >   COUNT                                            ~16     !2
         28        IS_SMALLER                                               !4, ~16
         29      > JMPNZ                                                    ~17, ->20
  109    30    >   FETCH_DIM_R                                      ~18     !3, !4
         31        VERIFY_RETURN_TYPE                                       ~18
         32      > RETURN                                                   ~18
  110    33*       VERIFY_RETURN_TYPE                                       
         34*     > RETURN                                                   null

End of function linear_search

Function some_weighted_random_pick:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 37
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 37
Branch analysis from position: 48
Branch analysis from position: 37
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
Branch analysis from position: 13
filename:       /in/PLjcH
function name:  some_weighted_random_pick
number of ops:  54
compiled vars:  !0 = $items, !1 = $count, !2 = $algo, !3 = $cumulative_weights, !4 = $total, !5 = $results, !6 = $i, !7 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  120     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
  122     3        ISSET_ISEMPTY_CV                                 ~8      !2
          4        BOOL_NOT                                         ~9      ~8
          5      > JMPZ                                                     ~9, ->13
  124     6    >   COUNT                                            ~10     !0
          7        IS_SMALLER                                               ~10, 50
          8      > JMPZ                                                     ~11, ->11
          9    >   QM_ASSIGN                                        ~12     'linear_search'
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~12     'binary_search'
         12    >   ASSIGN                                                   !2, ~12
  127    13    >   INIT_FCALL                                               'prepare_cumulative_weights'
         14        SEND_VAR                                                 !0
         15        DO_FCALL                                      0  $14     
         16        FETCH_LIST_R                                     $15     $14, 0
         17        ASSIGN                                                   !3, $15
         18        FETCH_LIST_R                                     $17     $14, 1
         19        ASSIGN                                                   !4, $17
         20        FREE                                                     $14
  130    21        INIT_FCALL                                               'array_combine'
         22        INIT_FCALL                                               'array_keys'
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                         $19     
         25        SEND_VAR                                                 $19
         26        INIT_FCALL                                               'array_fill'
         27        SEND_VAL                                                 0
         28        COUNT                                            ~20     !0
         29        SEND_VAL                                                 ~20
         30        SEND_VAL                                                 0
         31        DO_ICALL                                         $21     
         32        SEND_VAR                                                 $21
         33        DO_ICALL                                         $22     
         34        ASSIGN                                                   !5, $22
  133    35        ASSIGN                                                   !6, 0
         36      > JMP                                                      ->46
  134    37    >   INIT_FCALL                                               'weighted_random_pick'
         38        SEND_VAR                                                 !3
         39        SEND_VAR                                                 !4
         40        SEND_VAR                                                 !2
         41        DO_FCALL                                      0  $25     
         42        ASSIGN                                                   !7, $25
  135    43        FETCH_DIM_RW                                     $27     !5, !7
         44        PRE_INC                                                  $27
  133    45        PRE_INC                                                  !6
         46    >   IS_SMALLER                                               !6, !1
         47      > JMPNZ                                                    ~30, ->37
  137    48    >   INIT_STATIC_METHOD_CALL                                  'WeightRandomCacheStorage', 'clear'
         49        DO_FCALL                                      0          
  139    50        VERIFY_RETURN_TYPE                                       !5
         51      > RETURN                                                   !5
  140    52*       VERIFY_RETURN_TYPE                                       
         53*     > RETURN                                                   null

End of function some_weighted_random_pick

Function single_weighted_random_pick:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PLjcH
function name:  single_weighted_random_pick
number of ops:  22
compiled vars:  !0 = $items, !1 = $algo, !2 = $cumulative_weights, !3 = $total, !4 = $ret
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  149     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'binary_search'
  152     2        INIT_FCALL                                               'prepare_cumulative_weights'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $5      
          5        FETCH_LIST_R                                     $6      $5, 0
          6        ASSIGN                                                   !2, $6
          7        FETCH_LIST_R                                     $8      $5, 1
          8        ASSIGN                                                   !3, $8
          9        FREE                                                     $5
  155    10        INIT_FCALL                                               'weighted_random_pick'
         11        SEND_VAR                                                 !2
         12        SEND_VAR                                                 !3
         13        SEND_VAR                                                 !1
         14        DO_FCALL                                      0  $10     
         15        ASSIGN                                                   !4, $10
  156    16        INIT_STATIC_METHOD_CALL                                  'WeightRandomCacheStorage', 'clear'
         17        DO_FCALL                                      0          
  157    18        VERIFY_RETURN_TYPE                                       !4
         19      > RETURN                                                   !4
  158    20*       VERIFY_RETURN_TYPE                                       
         21*     > RETURN                                                   null

End of function single_weighted_random_pick

Function any_item_some_weighted_random_pick:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PLjcH
function name:  any_item_some_weighted_random_pick
number of ops:  26
compiled vars:  !0 = $items, !1 = $count, !2 = $weightGetter, !3 = $algo, !4 = $_items, !5 = $item, !6 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  192     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
          3        RECV_INIT                                        !3      null
  194     4        ASSIGN                                                   !4, <array>
  195     5        ISSET_ISEMPTY_CV                                         !2
          6      > JMPZ                                                     ~8, ->18
  196     7    > > FE_RESET_R                                       $9      !0, ->16
          8    > > FE_FETCH_R                                       ~10     $9, !5, ->16
          9    >   ASSIGN                                                   !6, ~10
  197    10        INIT_DYNAMIC_CALL                                        !2
         11        SEND_VAR_EX                                              !5
         12        DO_FCALL                                      0  $13     
         13        ASSIGN_DIM                                               !4, !6
         14        OP_DATA                                                  $13
  196    15      > JMP                                                      ->8
         16    >   FE_FREE                                                  $9
  195    17      > JMP                                                      ->19
  200    18    >   ASSIGN           

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
127.54 ms | 1498 KiB | 27 Q