3v4l.org

run code in 300+ PHP versions simultaneously
<?php //Unbounded knapsack problem //still need to figure out how many of each size $w=array(2,4,6); $w = array_map("round", $w);//$w needs to be integers, $v can be floats $v=array(2.2,4.6,6.8); $C=10;//max weight limit of knapsack $itemCount=array(); for($i=0;$i<count($C);$i++){ $itemCount[$i]=0; }; $knapsackResult=Knapsack($v,$w,$C); echo 'Knapsack is: '.$knapsackResult; echo '<pre>'; var_dump($itemCount); echo '</pre>'; echo 'elements are: <pre>'; $knapsackContentsResult=KnapsackContents($knapsackResult,$itemCount,$w,$v,$C); var_dump($knapsackContentsResult); echo '</pre>'; //(N = # different items) function Knapsack($v,$w,$C){ $sol=array(); $mySol=array(); $myFinalSol=array(); $N=count($v); /**************************************** Base case *************************************** */ if ($C == 0 ) return 0; /* ============================================== Divide and conquer procedure ============================================== */ /* --------------------------------------- Solve the appropriate smaller problems --------------------------------------- */ for ($i=0;$i<$N;$i++){ if($C>=$w[$i]){ $sol[$i] = Knapsack($v,$w,$C-$w[$i]);// Knapsack capacity reduced by w[i] } // because it has item i packed in else{$sol[$i] = 0;} // Not enough space to pack item i } /* --------------------------------------------- Use the solutions to the smaller problems to solve original problem --------------------------------------------- */ for($i=0;$i<$N;$i++){ if($C>=$w[$i]){ $mySol[$i]=$sol[$i]+$v[$i]; // Value is increased by v[i] } // because it has item i packed in // it already else{$mySol[$i]=0;} // Not enough space to pack item i } /* ************************* Find the best (maximum) ************************* */ $myFinalSol = $mySol[1]; for($i=0;$i<$N;$i++){ if($mySol[$i]>$myFinalSol){ $myFinalSol = $mySol[$i]; } } $GLOBALS["itemCount"][$C]=$myFinalSol; return $myFinalSol; }; function KnapsackContents($total,$itemCount,$w,$v,$C){ //$total is the total value that the knapsack function found //$itemCount is the value at each size of the knapsack $knapsackHolds=array(); $knapsackHoldsValue=array(); end($itemCount); $placeholder=key($itemCount);//get final key in array $v=array_reverse($v);//set value & weight to prefer largest $w=array_reverse($w); while($placeholder>0){ for($j=0;$j<count($v);$j++){ if($itemCount[$placeholder]-$v[$j]==$itemCount[$placeholder-$w[$j]]&&$itemCount[$placeholder-$w[$j]]>=0&&$itemCount[$placeholder]-$v[$j]){//if placeholder-weight of j equals placeholder - value of j, j is part of array $knapsackHolds[]=array('w'=>$w[$j],'v'=>$v[$j]); $placeholder-=$w[$j]; break; }; }; }; echo 'KnapsackContents dump: <pre>'; var_dump($knapsackHolds); echo '</pre>'; return $knapsackHolds; }; ?>

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.3.60.0070.01318.43
8.3.50.0100.01221.27
8.3.40.0110.00418.79
8.3.30.0090.00619.12
8.3.20.0070.00020.34
8.3.10.0040.00421.92
8.3.00.0000.00722.49
8.2.180.0130.00618.29
8.2.170.0090.00622.96
8.2.160.0090.00619.07
8.2.150.0080.00024.18
8.2.140.0080.00024.66
8.2.130.0090.00926.16
8.2.120.0000.00819.38
8.2.110.0060.00322.04
8.2.100.0060.00619.64
8.2.90.0000.00819.07
8.2.80.0050.00317.97
8.2.70.0000.00817.63
8.2.60.0030.00517.91
8.2.50.0050.00318.07
8.2.40.0080.00019.75
8.2.30.0000.00718.22
8.2.20.0040.00417.77
8.2.10.0040.00417.99
8.2.00.0030.00517.92
8.1.280.0070.00725.92
8.1.270.0080.00022.09
8.1.260.0080.00026.35
8.1.250.0080.00028.09
8.1.240.0030.00624.00
8.1.230.0060.00619.11
8.1.220.0040.00417.77
8.1.210.0040.00418.77
8.1.200.0100.00017.35
8.1.190.0040.00417.53
8.1.180.0000.00818.87
8.1.170.0050.00518.46
8.1.160.0000.00722.19
8.1.150.0040.00418.75
8.1.140.0000.00717.38
8.1.130.0030.00317.73
8.1.120.0030.00317.55
8.1.110.0000.00717.43
8.1.100.0080.00017.44
8.1.90.0000.00817.49
8.1.80.0030.00617.41
8.1.70.0030.00317.39
8.1.60.0030.00517.66
8.1.50.0000.00717.61
8.1.40.0040.00417.59
8.1.30.0040.00417.50
8.1.20.0040.00417.68
8.1.10.0040.00417.64
8.1.00.0040.00417.45
8.0.300.0040.00418.77
8.0.290.0050.00317.18
8.0.280.0040.00418.43
8.0.270.0000.00817.12
8.0.260.0030.00316.80
8.0.250.0040.00317.05
8.0.240.0070.00017.01
8.0.230.0030.00316.96
8.0.220.0000.00616.94
8.0.210.0080.00016.93
8.0.200.0050.00217.02
8.0.190.0080.00017.03
8.0.180.0040.00416.95
8.0.170.0040.00417.00
8.0.160.0000.00816.88
8.0.150.0000.00716.94
8.0.140.0050.00316.99
8.0.130.0030.00313.45
8.0.120.0040.00416.98
8.0.110.0000.00717.03
8.0.100.0000.00716.85
8.0.90.0040.00417.01
8.0.80.0060.01016.88
8.0.70.0040.00416.89
8.0.60.0040.00417.04
8.0.50.0050.00317.01
8.0.30.0050.01417.07
8.0.20.0050.01417.40
8.0.10.0000.00717.08
8.0.00.0070.01416.94
7.4.330.0040.00415.04
7.4.320.0040.00416.42
7.4.300.0000.01016.33
7.4.290.0070.00416.42
7.4.280.0000.01216.56
7.4.270.0000.01116.43
7.4.260.0060.00616.39
7.4.250.0210.00016.48
7.4.240.0050.01016.32
7.4.230.0000.00816.55
7.4.220.0070.03016.44
7.4.210.0050.01416.53
7.4.200.0000.00816.53
7.4.190.0060.00316.46
7.4.160.0470.01116.52
7.4.150.0240.01217.40
7.4.140.0360.01717.86
7.4.130.0230.02516.48
7.4.120.0260.00816.61
7.4.110.0330.02116.55
7.4.100.0660.03916.43
7.4.90.0220.01816.34
7.4.80.0750.02419.39
7.4.70.0320.01416.54
7.4.60.0170.02416.34
7.4.50.0110.01116.40
7.4.40.0200.02422.77
7.4.30.0410.02016.63
7.4.00.0040.01415.07
7.3.330.0031.99813.28
7.3.320.0002.00013.28
7.3.310.0070.00716.22
7.3.300.0000.01816.29
7.3.290.0220.02116.26
7.3.280.0180.01516.30
7.3.270.0230.02317.40
7.3.260.0240.02516.14
7.3.250.0340.01716.37
7.3.240.0150.02816.32
7.3.230.0210.00316.39
7.3.210.1090.01816.34
7.3.200.0190.02519.39
7.3.190.0350.02016.50
7.3.180.0240.02816.34
7.3.170.0220.02216.33
7.3.160.0310.00916.32
7.3.120.0070.01015.05
7.2.330.0240.01316.75
7.2.320.0330.01116.66
7.2.310.0210.02216.65
7.2.300.0350.02016.53
7.2.290.0970.02416.54
7.2.60.0120.00516.71
7.2.00.0120.00319.35
7.1.200.0060.00615.64
7.1.100.0090.00918.05
7.1.70.0070.00717.10
7.1.60.0080.01219.82
7.1.50.0070.01717.18
7.1.00.0030.08022.25
7.0.200.0040.00416.64
7.0.140.0100.06722.15
7.0.60.0100.08019.96
7.0.50.0100.08017.85
7.0.40.0070.08319.89
7.0.30.0230.05319.81
7.0.20.0270.08719.88
7.0.10.0100.08319.80
7.0.00.0000.04719.71
5.6.280.0100.06721.07
5.6.210.0130.07720.50
5.6.200.0100.08018.25
5.6.190.0030.08320.41
5.6.180.0470.07320.41
5.6.170.0270.05320.18
5.6.160.0030.07720.35
5.6.150.0070.04018.21
5.6.140.0100.04718.16
5.6.130.0130.06718.15
5.6.120.0030.04321.13
5.6.110.0070.08321.02
5.6.100.0100.08321.00
5.6.90.0130.07021.02
5.6.80.0070.07020.56
5.5.350.0130.08320.40
5.5.340.0030.08717.79
5.5.330.0130.06020.93
5.5.320.0200.09020.96
5.5.310.0170.05320.91
5.5.300.0100.06020.91
5.5.290.0170.06020.76
5.5.280.0170.05720.89
5.5.270.0200.07320.97
5.5.260.0130.06020.96
5.5.250.0070.06720.79
5.5.240.0100.06320.26
5.4.450.0170.06019.21
5.4.440.0200.05319.20
5.4.430.0230.05019.40
5.4.420.0130.05719.26
5.4.410.0130.05719.27
5.4.400.0200.05318.98
5.4.390.0130.08718.88
5.4.380.0170.05718.95
5.4.370.0100.06718.89
5.4.360.0070.06719.28
5.4.350.0100.05718.88
5.4.340.0270.05318.88
5.4.320.0230.05718.91
5.4.310.0000.06019.08
5.4.300.0200.05319.24
5.4.290.0230.05019.12
5.4.280.0100.06019.07
5.4.270.0100.06318.91
5.4.260.0100.05718.86
5.4.250.0100.06019.09
5.4.240.0100.06318.91
5.4.230.0070.06019.05
5.4.220.0230.05019.14
5.4.210.0200.06019.11
5.4.200.0170.05019.06
5.4.190.0230.05019.32
5.4.180.0100.07018.87
5.4.170.0170.06719.23
5.4.160.0130.05719.10
5.4.150.0100.05719.06
5.4.140.0100.05316.36
5.4.130.0100.05716.59
5.4.120.0130.07316.46
5.4.110.0170.05316.48
5.4.100.0100.05316.51
5.4.90.0100.07016.44
5.4.80.0100.05716.19
5.4.70.0200.04316.35
5.4.60.0130.05716.34
5.4.50.0070.06016.44
5.4.40.0130.05316.27
5.4.30.0200.07016.52
5.4.20.0170.07316.50
5.4.10.0270.06316.54
5.4.00.0130.07315.93
5.3.290.0100.06714.57
5.3.280.0200.05714.50
5.3.270.0070.06714.42
5.3.260.0170.07014.51
5.3.250.0030.06714.48
5.3.240.0100.05314.46
5.3.230.0070.05314.50
5.3.220.0130.05014.47
5.3.210.0100.05714.47
5.3.200.0230.07014.49
5.3.190.0230.05714.29
5.3.180.0070.06714.47
5.3.170.0130.04714.44
5.3.160.0070.07014.47
5.3.150.0130.05314.47
5.3.140.0100.05014.31
5.3.130.0130.08314.45
5.3.120.0070.06014.48
5.3.110.0130.05314.40
5.3.100.0070.07013.93
5.3.90.0230.05014.11
5.3.80.0200.05313.97
5.3.70.0170.05313.85
5.3.60.0170.07313.81
5.3.50.0070.05313.77
5.3.40.0100.05013.73
5.3.30.0100.07713.74
5.3.20.0100.05313.50
5.3.10.0100.06013.47
5.3.00.0130.05013.59
5.2.170.0070.04310.96
5.2.160.0070.05310.95
5.2.150.0100.04010.95
5.2.140.0070.05010.80
5.2.130.0130.04010.90
5.2.120.0170.06010.92
5.2.110.0100.05010.87
5.2.100.0100.06310.87
5.2.90.0100.05310.77
5.2.80.0170.05310.91
5.2.70.0170.03710.96
5.2.60.0030.06010.99
5.2.50.0200.03710.72
5.2.40.0100.05010.71
5.2.30.0030.05010.81
5.2.20.0100.04710.96
5.2.10.0130.04010.59
5.2.00.0100.04710.64
5.1.60.0000.0539.93
5.1.50.0100.0479.87
5.1.40.0070.0479.89
5.1.30.0100.04710.23
5.1.20.0100.05310.23
5.1.10.0170.0539.97
5.1.00.0130.04310.00
5.0.50.0130.04710.28
5.0.40.0170.0508.37
5.0.30.0070.05710.28
5.0.20.0100.0578.08
5.0.10.0070.03710.28
5.0.00.0070.0638.11
4.4.90.0030.0378.84
4.4.80.0030.0538.84
4.4.70.0030.0378.84
4.4.60.0170.02010.28
4.4.50.0070.0338.84
4.4.40.0000.04310.28
4.4.30.0030.02710.28
4.4.20.0070.02710.28
4.4.10.0000.03010.28
4.4.00.0130.04310.28
4.3.110.0100.0278.02
4.3.100.0070.0378.02
4.3.90.0100.0408.02
4.3.80.0070.0578.02
4.3.70.0070.02710.28
4.3.60.0100.0338.02
4.3.50.0130.03010.28
4.3.40.0030.0507.99
4.3.30.0030.0407.99
4.3.20.0070.02710.28
4.3.10.0070.0337.99
4.3.00.0070.0407.99

preferences:
47.83 ms | 400 KiB | 5 Q