3v4l.org

run code in 300+ PHP versions simultaneously
<?php function randomizeTeam( int $teamSize, array $positionPool, array $positionMins = [], array $positionMaxs = [] ): array { // guard conditions foreach ($positionMins as $pos => $min) { if (!key_exists($pos, $positionPool) || $positionPool[$pos] < $min) { throw new Exception('Position pool cannot accommodate minimum required positions'); } } // input preparation foreach ($positionMaxs as $pos => $max) { if (key_exists($pos, $positionPool) && $positionPool[$pos] > $max) { $positionPool[$pos] = $max; } } $team = []; $sum = 0; foreach ($positionPool as $pos => &$pool) { $team[$pos] = $positionMins[$pos] ?? 0; $pool -= $team[$pos]; if (!$pool) { unset($positionPool[$pos]); // remove exhausted pool } $sum += $team[$pos]; } while ($sum < $teamSize && $positionPool) { $pos = array_rand($positionPool); ++$team[$pos]; --$positionPool[$pos]; if (!$positionPool[$pos]) { unset($positionPool[$pos]); // remove exhausted pool } ++$sum; } if (array_sum($team) < $teamSize) { throw new Exception('Position pool was exhausted before filling team'); } return $team; } var_export( randomizeTeam( 11, [ 'wicket_keeper' => 2, 'batsman' => 6, 'all_rounders' => 5, 'bowlers' => 5, ], [ 'wicket_keeper' => 1, 'batsman' => 3, 'all_rounders' => 1, 'bowlers' => 2, ], [ 'wicket_keeper' => 3, 'batsman' => 5, 'all_rounders' => 5, 'bowlers' => 4, ] ) );

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.2.110.0070.01322.32
8.2.100.0150.00720.93
8.2.90.0300.00318.91
8.2.80.0180.00718.56
8.2.70.0260.00718.60
8.2.60.0170.00018.91
8.2.50.0130.00019.93
8.2.40.0090.00418.96
8.2.30.0110.00318.80
8.2.20.0030.01018.78
8.2.10.0130.00318.93
8.2.00.0110.00319.03
8.1.240.0160.00018.63
8.1.230.0130.00318.61
8.1.220.0060.00918.62
8.1.210.0090.00419.69
8.1.200.0070.00718.68
8.1.190.0120.00418.55
8.1.180.0160.00018.50
8.1.170.0130.00018.53
8.1.160.0120.00018.73
8.1.150.0040.00820.54
8.1.140.0100.00318.55
8.1.130.0150.00018.60
8.1.120.0040.00818.50
8.1.110.0120.00019.76
8.1.100.0110.00318.55
8.1.90.0130.00018.50
8.1.80.0100.00318.54
8.1.70.0060.00618.88
8.1.60.0090.00618.83
8.1.50.0110.00418.70
8.1.40.0120.00018.78
8.1.30.0130.00018.99
8.1.20.0130.00318.74
8.1.10.0130.00018.59
8.1.00.0060.00618.46
8.0.300.0110.00417.78
8.0.290.0220.00317.42
8.0.280.0190.01117.84
8.0.270.0120.00417.80
8.0.260.0030.00718.45
8.0.250.0130.00317.88
8.0.240.0100.00717.72
8.0.230.0060.00618.24
8.0.220.0080.00817.85
8.0.210.0070.00717.70
8.0.200.0090.00617.80
8.0.190.0060.00618.05
8.0.180.0070.00717.92
8.0.170.0100.00517.88
8.0.160.0130.00318.07
8.0.150.0130.00317.82
8.0.140.0160.00017.82
8.0.130.0090.00518.24
8.0.120.0160.00017.74
8.0.110.0090.00617.95
8.0.100.0100.00617.70
8.0.90.0120.00417.78
8.0.80.0120.00317.88
8.0.70.0110.00417.86
8.0.60.0130.00018.17
8.0.50.0080.00817.88
8.0.30.0130.00017.91
8.0.20.0130.00417.74
8.0.10.0040.01117.98

preferences:
35.74 ms | 403 KiB | 5 Q