3v4l.org

run code in 300+ PHP versions simultaneously
<?php $valueWeights = [ 149 => 55.555, 130 => 10.0050, 131 => 5, 132 => 5.2, 133 => 10, 134 => 10.24, 135 => 5 ]; $mostDecimals = 0; // not bothering to validate against infinite and extremely fringe case floats foreach ($valueWeights as $value => $weight) { $tempDecimals = 0; while ((string)$weight !== (string)floor($weight)) { $weight *= 10; // this is not permanently mutating the weight ++$tempDecimals; } $mostDecimals = max($mostDecimals, $tempDecimals); } echo "Most Decimals: {$mostDecimals}\n"; $factor = pow(10, $mostDecimals); echo "Factor: " , $factor , "\n"; $totalWeight = (array_sum($valueWeights) - 1) * $factor; for ($i = 0; $i < 10; ++$i) { $rand = mt_rand(0, $totalWeight); echo "\nRand: " , $rand , "\n"; $cumulativeScaledWeight = 0; foreach ($valueWeights as $value => $weight) { $cumulativeScaledWeight += $weight * $factor; if ($rand < $cumulativeScaledWeight) { echo "Value: {$value}\n"; break; } } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.4.40.0060.01216.45
7.4.30.0080.01216.41
7.4.20.0090.00916.46
7.4.10.0110.00716.60
7.4.00.0110.00716.54
7.3.160.0100.01016.27
7.3.150.0100.00716.73
7.3.140.0130.01016.53
7.3.130.0150.00416.32
7.3.120.0140.00516.27
7.3.110.0070.01016.65
7.3.100.0180.00016.36
7.3.90.0090.00916.50
7.3.80.0030.01416.29
7.3.70.0090.01516.30
7.3.60.0110.00816.59
7.3.50.0030.01316.53
7.3.40.0090.00916.43
7.3.30.0040.01916.35
7.3.20.0030.01516.44
7.3.10.0120.01216.48
7.3.00.0110.00616.39
7.2.290.0150.00416.74
7.2.280.0140.00616.56
7.2.270.0040.01416.68
7.2.260.0060.01316.63
7.2.250.0120.00916.49
7.2.240.0040.01516.64
7.2.230.0060.01116.55
7.2.220.0100.01016.70
7.2.210.0210.00316.44
7.2.200.0080.01016.66
7.2.190.0090.01216.48
7.2.180.0040.01616.48
7.2.170.0110.00716.51
7.2.160.0070.01016.63
7.2.150.0060.01216.86
7.2.140.0100.01116.68
7.2.130.0100.00816.72
7.2.120.0090.01216.59
7.2.110.0150.00316.81
7.2.100.0090.00916.83
7.2.90.0060.01616.70
7.2.80.0100.01016.54
7.2.70.0090.00916.36
7.2.60.0110.00916.61
7.2.50.0110.00616.67
7.2.40.0060.01416.69
7.2.30.0090.00916.65
7.2.20.0080.01216.73
7.2.10.0140.00316.68
7.2.00.0130.00716.68

preferences:
43.79 ms | 403 KiB | 5 Q