3v4l.org

run code in 300+ PHP versions simultaneously
<?php function intdiv_1($a, $b){ return ($a - $a % $b) / $b; } function int_div($dividend, $divisor){ $res; if (function_exists('intdiv')) $res=intdiv($dividend, $divisor); else // < php7 $res=intdiv_1($dividend, $divisor); return $res; } class Sheet { private $size; //1,2 or 4 private $sections=array()/* = array(4) { "1"=>null, "2"=>null, "3"=>null, "4"=>null }*/; public function __construct($dim, $arg) { $this->size = $dim; if (is_array($arg)==true && sizeof($arg)==$dim) {//building a sheet of 2 or 4 $i=1; foreach($arg as $elem) $this->sections[$i++]=$elem; return; } else { if (is_array($arg)==false && $dim==1) //buidling a sheet of 1 $this->sections[1]= $arg; else throw new Exception("Incompatible arguments"); } } public function __toString() { $ret='-'; foreach($this->sections as $key=>$val) $ret .= (string)$val; return $ret.'-'; } public function divide(){ //return first the left or upper section of the shhet $ret1; //left or upper part of the sheet $ret2; //right or lower part of the shet switch($this->size) { case 4: //vertical cut $arr1=array($this->sections[1],$this->sections[3]); $ret1=new Sheet(2,$arr1); $arr2=array($this->sections[2],$this->sections[4]); $ret2=new Sheet(2,$arr2); break; case 2: //horizontal cut $ret1 = new Sheet(1,$this->sections[1]); $ret2 = new Sheet(1,$this->sections[2]); break; default: throw new Exception("Value must be 2 or 4"); } $this->size /= 2; return array($ret1,$ret2); } public function toArray(){ $ret=[]; foreach($this->sections as $val) $ret[]=$val; return $ret; } } class SheetPile{ private $sheets=array(); private $sheetSize; public function __construct($size){ $this->sheetSize=$size; } public function __toString() { $ret='A stack of '.$this->sheetSize."-sized sheets:\n"; foreach($this->sheets as $val) $ret .= (string)$val."\n"; return $ret; } public function add($sheet){ //sheets are stacked one below the other starting from the upper one array_push($this->sheets, $sheet); } public function divide(){ //return first the left or upper section of the pile in form of SheetPiles $pile1=new SheetPile($this->sheetSize/2); $pile2=new SheetPile($this->sheetSize/2); switch($this->sheetSize) { case 2:case 4: //horizontal(2) or vertical(4) cut foreach($this->sheets as $elem){ list($s1,$s2)= $elem->divide(); $pile1->add($s1); $pile2->add($s2); } break; default: throw new Exception("Value must be 2 or 4"); } return array($pile1,$pile2); } public function stackUp(){ //left pile ($this) is placed over the right one, upper pile ($this) over the other list($pile1,$pile2)=$this->divide(); $pile1->sheets = array_merge($pile1->sheets,$pile2->sheets); return $pile1; } public function SingleSheetPile_To_Array(){ if ($this->sheetSize!=1) throw new Exception("This function applies only to Pile of Sheets with size 1"); else { $ret=[]; foreach($this->sheets as $elem) $ret=array_merge($ret,$elem->toArray()); return $ret; } } } function A4toA6($num_pages) { $n =1+int_div($num_pages-1, 4); $arr; $pp=1; for($i=1;$i<=$n;$i++){ for($j=0;$j<4;$j++){ $val=$i+$j*$n; $arr[$i][$j]=($val>$num_pages)?" ":$val; //arr[page][quarter(0-3 from left to right from top to bottom] } } return $arr; } function verify_A4toA6($num_pages) { $arr=A4toA6($num_pages); $pile=new SheetPile(4); foreach($arr as $val){ //for every page $sheet4 = new Sheet(4,$val); $pile->add($sheet4); } $pile=$pile->stackUp(); //from 4 to 2 $pile=$pile->stackUp(); //from 2 to 1 $pages=[]; for($i=1;$i<=$num_pages;$i++) $pages[]=$i; return $pages==$pile->SingleSheetPile_To_Array(); } print_r(3==3);print_r(3==33); print("SHEEEEEET\n"); $s1 = new Sheet(1,0);print($s1."\n"); $s2 = new Sheet(2,array(2,4));print($s2."\n"); list($a,$b)=$s2->divide();print("V:\n");print($a."\n");print($b); $s4 = new Sheet(4,array(3,5,7,9));print("\n".$s4."\n"); print("sheetdiv\n"); list($a,$b)=$s4->divide();print("\nH:\n");print($a."\n");print($b); print("PILEEEEEE\n"); $q1 = new Sheet(4,array(1,3,5,7));$q2 = new Sheet(4,array(2,4,6,8)); $p = new SheetPile(4); $p->add($q1);$p->add($q2);print($p); $d1=new Sheet(2,array(2,4)); $d2 = new Sheet(2,array(7,8)); $pp = new SheetPile(2);$pp->add($d1);$pp->add($d2);print($pp); $u1=new Sheet(1,4); $u2=new Sheet(1,1); $ppp = new SheetPile(1);$ppp->add($u1);$ppp->add($u2);print($ppp); print("pilediv\n"); print_r($p->stackUp()->stackUp()->SingleSheetPile_To_Array()); print_r(verify_A4toA6(8));

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.0140.00418.18
8.3.50.0140.00718.07
8.3.40.0120.00418.98
8.3.30.0130.00719.02
8.3.20.0080.00020.46
8.3.10.0030.00523.36
8.3.00.0080.00019.68
8.2.180.0130.01016.83
8.2.170.0120.00322.96
8.2.160.0110.00719.41
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0030.00620.38
8.2.120.0040.00426.35
8.2.110.0000.01020.96
8.2.100.0040.00817.91
8.2.90.0080.00018.16
8.2.80.0060.00317.97
8.2.70.0060.00318.05
8.2.60.0040.00917.91
8.2.50.0030.00518.34
8.2.40.0060.00620.72
8.2.30.0120.00019.29
8.2.20.0000.00818.16
8.2.10.0000.00818.16
8.2.00.0050.00318.30
8.1.280.0070.01125.92
8.1.270.0060.00322.31
8.1.260.0060.00326.35
8.1.250.0040.00428.09
8.1.240.0030.00722.04
8.1.230.0040.00822.75
8.1.220.0080.00017.89
8.1.210.0040.00418.77
8.1.200.0030.00617.48
8.1.190.0060.00317.48
8.1.180.0030.00618.10
8.1.170.0080.00018.57
8.1.160.0040.00419.00
8.1.150.0050.00220.38
8.1.140.0040.00417.76
8.1.130.0040.00419.18
8.1.120.0080.00017.48
8.1.110.0050.00317.51
8.1.100.0050.00517.48
8.1.90.0040.00417.49
8.1.80.0040.00417.48
8.1.70.0000.00717.57
8.1.60.0000.00817.65
8.1.50.0060.00317.62
8.1.40.0080.00017.63
8.1.30.0000.00817.81
8.1.20.0000.00817.81
8.1.10.0030.00617.73
8.1.00.0000.00817.57
8.0.300.0040.00419.77
8.0.290.0080.00016.75
8.0.280.0040.00418.64
8.0.270.0000.00717.37
8.0.260.0000.00716.87
8.0.250.0030.00317.10
8.0.240.0030.00316.95
8.0.230.0040.00417.07
8.0.220.0030.00317.04
8.0.210.0030.00516.97
8.0.200.0000.00717.08
8.0.190.0040.00417.03
8.0.180.0040.00416.91
8.0.170.0030.00617.09
8.0.160.0040.00417.02
8.0.150.0000.00717.05
8.0.140.0030.00516.98
8.0.130.0000.00613.36
8.0.120.0030.00616.86
8.0.110.0040.00417.00
8.0.100.0000.00816.82
8.0.90.0040.00416.93
8.0.80.0000.01717.02
8.0.70.0050.00316.81
8.0.60.0040.00416.91
8.0.50.0040.00417.12
8.0.30.0110.00817.19
8.0.20.0080.01317.41
8.0.10.0050.00316.98
8.0.00.0140.00616.87
7.4.330.0050.00015.55
7.4.320.0030.00316.71
7.4.300.0050.00316.64
7.4.290.0000.00816.60
7.4.280.0000.00916.72
7.4.270.0040.00816.56
7.4.260.0000.00716.61
7.4.250.0030.00816.70
7.4.240.0070.00316.73
7.4.230.0050.00216.70
7.4.220.0030.00616.46
7.4.210.0060.01116.71
7.4.200.0000.00716.84
7.4.160.0100.01016.67
7.4.150.0030.01517.40
7.4.140.0090.00917.86
7.4.130.0120.00916.50
7.4.120.0130.00816.67
7.4.110.0110.00716.85
7.4.100.0090.00916.53
7.4.90.0100.01016.57
7.4.80.0170.00519.39
7.4.70.0070.01116.64
7.4.60.0060.00916.61
7.4.50.0060.01316.49
7.4.40.0070.01016.46
7.4.10.0100.00714.96
7.4.00.0110.00815.28
7.3.330.0000.00513.48
7.3.320.0030.00313.41
7.3.310.0030.00516.54
7.3.300.0070.00016.47
7.3.290.0040.00416.44
7.3.280.0110.00416.48
7.3.270.0110.00717.40
7.3.260.0100.01216.59
7.3.250.0070.01416.58
7.3.240.0110.01016.55
7.3.230.0040.01516.58
7.3.210.0090.00916.55
7.3.200.0090.00916.57
7.3.190.0030.01716.65
7.3.180.0070.01016.42
7.3.170.0140.00716.64
7.3.160.0130.00916.45
7.3.130.0100.00715.08
7.3.120.0160.00315.20
7.3.110.0100.01014.99
7.3.100.0090.00614.82
7.3.90.0040.00814.88
7.3.80.0040.00714.79
7.3.70.0030.00614.99
7.3.60.0090.00314.93
7.3.50.0080.00814.98
7.3.40.0060.00615.02
7.3.30.0070.01015.06
7.3.20.0060.00616.71
7.3.10.0070.00716.67
7.3.00.0030.01316.83
7.2.330.0070.01116.97
7.2.320.0060.01216.82
7.2.310.0090.00916.89
7.2.300.0140.00616.93
7.2.290.0120.00616.76
7.2.260.0060.01315.14
7.2.250.0060.01215.32
7.2.240.0040.01115.23
7.2.230.0030.01315.06
7.2.220.0070.01115.30
7.2.210.0040.01115.11
7.2.200.0000.01215.15
7.2.190.0000.01115.22
7.2.180.0030.00615.27
7.2.170.0030.01015.04
7.2.160.0060.01015.11
7.2.150.0030.01216.98
7.2.140.0030.00617.11
7.2.130.0060.01017.05
7.2.120.0070.00716.90
7.2.110.0100.00317.02
7.2.100.0000.01216.76
7.2.90.0000.01616.98
7.2.80.0000.01216.63
7.2.70.0030.01216.71
7.2.60.0030.00816.71
7.2.50.0030.01016.95
7.2.40.0100.00917.02
7.2.30.0160.00017.04
7.2.20.0040.01518.45
7.2.10.0030.01318.37
7.2.00.0110.00818.29
7.1.330.0000.01115.73
7.1.320.0030.00715.97
7.1.310.0060.00915.95
7.1.300.0070.00715.87
7.1.290.0000.00815.80
7.1.280.0030.00915.83
7.1.270.0040.01115.82
7.1.260.0090.00315.87
7.1.250.0060.00915.61
7.1.240.0080.00615.88
7.1.230.0100.00415.72
7.1.220.0060.00615.63
7.1.210.0100.00715.68
7.1.200.0090.00615.62
7.1.190.0040.00715.77
7.1.180.0040.00815.80
7.1.170.0030.01015.49
7.1.160.0100.00315.86
7.1.150.0060.01015.54
7.1.140.0040.01317.28
7.1.130.0070.00917.17
7.1.120.0090.00717.40
7.1.110.0020.01416.89
7.1.100.0040.01317.08
7.1.90.0030.01416.96
7.1.80.0120.00517.10
7.1.70.0070.00716.44
7.1.60.0160.00825.47
7.1.50.0130.01625.38
7.1.40.0120.01425.24
7.1.30.0100.01725.05
7.1.20.0150.01025.33
7.1.10.0060.01016.23
7.1.00.0090.00716.12
7.0.330.0060.00815.51
7.0.320.0000.01415.25
7.0.310.0090.00315.43
7.0.300.0120.00615.17
7.0.290.0060.00615.39
7.0.280.0110.00315.13
7.0.270.0060.00815.34
7.0.260.0100.00615.38
7.0.250.0030.00715.40
7.0.240.0090.00315.48
7.0.230.0040.00815.29
7.0.220.0110.00415.36
7.0.210.0040.01415.60
7.0.200.0070.00415.36
7.0.190.0030.01415.50
7.0.180.0090.00615.51
7.0.170.0120.00615.40
7.0.160.0070.00715.55
7.0.150.0040.01115.59
7.0.140.0030.01315.56
7.0.130.0030.00615.17
7.0.120.0040.00415.22
7.0.110.0030.00615.60
7.0.100.0070.00715.36
7.0.90.0070.00715.41
7.0.80.0060.00915.41
7.0.70.0070.00715.59
7.0.60.0040.01115.38
7.0.50.0030.00915.70
7.0.40.0090.00613.30
7.0.30.0080.00613.57
7.0.20.0110.00313.57
7.0.10.0060.00913.59
7.0.00.0040.00413.55
5.6.400.0040.01114.50
5.6.390.0030.00714.75
5.6.380.0030.01014.39
5.6.370.0090.00014.74
5.6.360.0030.01314.45
5.6.350.0030.01314.63
5.6.340.0080.00514.49
5.6.330.0090.00914.60
5.6.320.0060.00614.38
5.6.310.0040.01114.68
5.6.300.0000.01414.38
5.6.290.0060.00914.90
5.6.280.0100.00314.45
5.6.270.0040.00714.83
5.6.260.0090.00414.29
5.6.250.0030.01014.58
5.6.240.0090.00914.35
5.6.230.0090.00014.93
5.6.220.0070.01014.36
5.6.210.0070.00414.70
5.6.200.0030.00714.84
5.6.190.0030.01014.62
5.6.180.0040.01414.58
5.6.170.0040.01114.59
5.6.160.0090.00614.60
5.6.150.0040.01114.50
5.6.140.0080.00514.44
5.6.130.0100.00314.72
5.6.120.0080.00514.67
5.6.110.0040.01414.40
5.6.100.0030.00814.67
5.6.90.0040.00914.52
5.6.80.0030.01014.34
5.6.70.0060.00914.62
5.6.60.0060.01014.55
5.6.50.0040.01514.59
5.6.40.0130.00314.39
5.6.30.0000.01014.49
5.6.20.0060.00614.46
5.6.10.0040.00714.68
5.6.00.0060.00614.48

preferences:
65.61 ms | 401 KiB | 5 Q