3v4l.org

run code in 300+ 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);

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.1.71.0461.41267.65
7.1.60.5721.92567.99
7.1.50.1590.12767.66
7.1.00.0631.92772.91
7.0.201.8820.53263.53
7.0.140.1031.86372.54
7.0.101.1601.34068.29
7.0.91.0031.49768.25
7.0.80.9831.51768.30
7.0.71.1731.32768.23
7.0.61.1131.38768.34
7.0.51.0501.45068.70
7.0.41.1931.30727.53
7.0.31.0201.48027.59
7.0.21.0231.47727.52
7.0.11.2231.27727.46
7.0.01.1631.32027.60
5.6.250.0330.20085.20
5.6.240.0270.07385.23
5.6.230.0300.12085.31
5.6.220.0200.15085.24
5.6.210.0200.09785.09
5.6.200.0370.15385.70
5.6.190.0430.13785.66
5.6.180.0270.16385.57
5.6.170.0400.14785.64
5.6.160.0270.14085.70
5.6.150.0330.10085.60
5.6.140.0130.15785.68
5.6.130.0200.12385.80
5.6.120.0300.08785.77
5.6.110.0330.08385.64
5.6.100.0400.07785.61
5.6.90.0270.10085.56
5.6.80.0300.09385.04
5.6.70.0100.10385.09
5.6.60.0170.09785.12
5.6.50.0170.09785.05
5.6.40.0370.08085.00
5.6.30.0200.09385.04
5.6.20.0200.09384.99
5.6.10.0130.09785.07
5.6.00.0170.09384.98
5.5.380.0270.08385.07
5.5.370.0270.12085.05
5.5.360.0100.15085.07
5.5.350.0230.09085.10
5.5.340.0270.15785.52
5.5.330.0330.15085.51
5.5.320.0400.13785.41
5.5.310.0270.12085.56
5.5.300.0230.15785.52
5.5.290.0500.14085.51
5.5.280.0400.13085.43
5.5.270.0230.10085.50
5.5.260.0200.08085.41
5.5.250.0230.11085.28
5.5.240.0400.09784.75
5.5.230.0300.08784.88
5.5.220.0270.09084.93
5.5.210.0300.08784.82
5.5.200.0270.08784.77
5.5.190.0300.09384.71
5.5.180.0070.10784.87
5.5.160.0370.11084.77
5.5.150.0230.14084.81
5.5.140.0300.15084.90
5.5.130.0330.13384.86
5.5.120.0300.11784.84
5.5.110.0230.13384.85
5.5.100.0500.14084.70
5.5.90.0170.11784.74
5.5.80.0100.12384.64
5.5.70.0200.16384.57
5.5.60.0230.09084.69
5.5.50.0300.10084.78
5.5.40.0300.15084.75
5.5.30.0300.13784.77
5.5.20.0300.11784.71
5.5.10.0300.14784.64
5.5.00.0400.15084.68
5.4.450.0230.13784.11
5.4.440.0300.16383.92
5.4.430.0230.09383.73
5.4.420.0130.10783.93
5.4.410.0200.09084.00
5.4.400.0170.09383.64
5.4.390.0300.09083.75
5.4.380.0170.10083.45
5.4.370.0130.11383.65
5.4.360.0270.07783.50
5.4.350.0200.09083.60
5.4.340.0200.10783.56
5.4.320.0300.14783.66
5.4.310.0270.09083.62
5.4.300.0130.14783.65
5.4.290.0200.14783.78
5.4.280.0430.13783.68
5.4.270.0270.14083.67
5.4.260.0100.10383.58
5.4.250.0370.08783.55
5.4.240.0200.12383.62
5.4.230.0200.09383.62
5.4.220.0200.16783.62
5.4.210.0230.16083.65
5.4.200.0270.15383.58
5.4.190.0330.15783.54
5.4.180.0300.15383.45
5.4.170.0300.13783.77
5.4.160.0300.11783.61
5.4.150.0330.13783.44
5.4.140.0670.06781.02
5.4.130.0330.13381.02
5.4.120.0270.11080.80
5.4.110.0300.14381.08
5.4.100.0270.12781.00
5.4.90.0130.15081.07
5.4.80.0370.11781.06
5.4.70.0170.16781.06
5.4.60.0200.13080.92
5.4.50.0200.14780.98
5.4.40.0300.08081.05
5.4.30.0370.12080.86
5.4.20.0170.09380.94
5.4.10.0400.12080.99
5.4.00.0170.09780.48

preferences:
47.79 ms | 403 KiB | 5 Q