3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_rand_cryptographically_secure(array $array): int|string { $max = count($array) - 1; if ($max < 0) { throw new ValueError ('Argument #1 ($array) cannot be empty'); } if (function_exists('array_is_list') && array_is_list($array)) { // optimization, this avoids creating a copy of all the keys return random_int(0, $max); } return array_keys($array)[random_int(0, $max)]; } $tests = [ [5, 6, 7], ['a' => 1, 'b' => 2, 'c' => 3], ['zero', 4 => 'four', 9 => 'nine'] ]; foreach ($tests as $test) { echo array_rand_cryptographically_secure($test) . "\n"; }

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)
8.1.50.0060.00617.46
8.1.40.0170.00717.37
8.1.30.0220.00417.57
8.1.20.0190.00717.60
8.1.10.0060.00317.48
8.1.00.0160.00917.36
8.0.180.0070.00416.84
8.0.170.0140.00516.82
8.0.160.0200.00016.84
8.0.150.0140.00716.83
8.0.140.0120.00616.82
8.0.130.0170.00316.61
8.0.120.0170.00416.86
8.0.110.0150.00516.66
8.0.100.0120.00816.86
8.0.90.0130.00716.73
8.0.80.0190.00316.90
8.0.70.0180.00316.76
8.0.60.0200.00316.93
8.0.50.0230.00016.86
8.0.30.0170.00716.73
8.0.20.0170.00616.79
8.0.10.0110.01116.88
7.4.290.0030.00516.32
7.4.280.0200.00616.31
7.4.270.0150.00916.23
7.4.260.0160.00816.36
7.4.250.0200.00516.30
7.4.240.0160.00616.21
7.4.230.0140.00716.44
7.4.220.0090.00516.17
7.4.210.0000.00916.41
7.4.200.0140.00416.35
7.4.190.0120.00816.39
7.4.180.0130.00816.36
7.4.160.0170.00516.27
7.4.150.0170.00416.29
7.4.140.0160.00616.13
7.4.130.0210.00016.11
7.4.120.0190.00316.23
7.4.110.0160.00616.21
7.4.100.0170.00316.29
7.4.90.0210.00016.08
7.4.80.0200.00015.75
7.4.70.0200.00316.31
7.4.60.0210.00316.01
7.4.50.0170.00316.21
7.4.40.0150.00616.03
7.4.30.0170.00516.16
7.4.20.0190.00315.96
7.4.10.0130.00916.08
7.4.00.0180.00316.25

preferences:
36.57 ms | 401 KiB | 5 Q