3v4l.org

run code in 300+ PHP versions simultaneously
<?php for($i=$x=0; $i<=500000; ++$i){ $limits_and_values[$x+=rand(1,10)]=$i; } $start = microtime(true); print "weighted sum is $x\n"; $pick=mt_rand(1,$x); // pull random integer between 1 and highest limit/key while(!isset($limits_and_values[$pick])){++$pick;} // smallest possible loop to find key $end = microtime(true); print "selected value is: $limits_and_values[$pick]\n"; print "total time is " . ($end - $start) . "\n";

preferences:
38.26 ms | 406 KiB | 5 Q