3v4l.org

run code in 300+ PHP versions simultaneously
<?php $max_weight = 100; // init with some random weights $objects = []; for ($i = 1; $i <= 100; $i++) { $objects[] = (object) ['id' => $i, 'weight' => rand(1, 50)]; } // sort by weight, heaviest first usort($objects, function ($a, $b) { return $b->weight <=> $a->weight; }); $containers = []; $container_number = 0; $total_weight = 0; while (true) { $container_number++; $container = []; $container_weight = 0; foreach ($objects as $key => $object) { // add this object to the container if it won't push it over the max weight if ($container_weight + $object->weight <= $max_weight) { $container[] = $object; unset($objects[$key]); $container_weight += $object->weight; $total_weight += $object->weight; if ($container_weight == $max_weight) { break; } } } $containers[$container_number] = $container; $container_contents = count($container); echo 'Container #' . $container_number . ' total weight: ' . $container_weight . ' (' . $container_contents . ' items)' . PHP_EOL; if (empty($objects)) { break; } } echo 'Total weight: ' . $total_weight . PHP_EOL;

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.0.10.0150.00417.11
8.0.00.0130.00717.03
7.4.140.0150.00316.68
7.4.130.0120.00616.52
7.4.120.0110.00816.42
7.4.110.0140.00416.66
7.4.100.0140.00716.48
7.4.90.0060.01216.50
7.4.80.0150.00416.36
7.4.70.0110.00816.34
7.4.60.0090.00916.34
7.4.50.0060.01116.29
7.4.40.0080.01116.26
7.4.30.0280.01316.59
7.4.20.0070.01116.64
7.4.10.0090.00916.43
7.4.00.0080.01416.42
7.3.260.0070.01116.48
7.3.250.0100.01016.34
7.3.240.0140.01016.51
7.3.230.0100.00716.59
7.3.220.0200.00316.39
7.3.210.0130.01016.63
7.3.200.0120.00616.55
7.3.190.0140.00416.54
7.3.180.0090.00916.37
7.3.170.0130.00716.39
7.3.160.0070.01316.65
7.3.150.0100.00716.66
7.3.140.0130.00616.57
7.3.130.0130.01016.57
7.3.120.0110.01116.69
7.3.110.0030.01416.41
7.3.100.0100.00716.59
7.3.90.0040.01516.36
7.3.80.0100.00716.34
7.3.70.0160.00416.30
7.3.60.0100.00716.42
7.3.50.0100.00716.55
7.3.40.0130.00416.45
7.3.30.0100.00716.53
7.3.20.0140.00716.31
7.3.10.0130.01016.47
7.3.00.0030.01816.31

preferences:
53.76 ms | 403 KiB | 5 Q