3v4l.org

run code in 300+ PHP versions simultaneously
<?php $matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], // [17, 18, 19, 20] ]; $r = 1; function matrixRotation($matrix, $r) { $m = new Matrix($matrix); $m->rotate(); } class Matrix { private $mx; private $m; private $n; private $a; // i [a,b] private $b; private $c; // j [c,d] private $d; public function get() { return $this->mx; } public function __construct($mx) { try { $this->set($mx); } catch(\Exception $e) { echo $e->getMessage(); // exit(1); } } private function set(array $mx) { $this->m = count($mx); $this->n = count($mx[0]); $this->a = 0; $this->b = m-1; $this->c = 0; $this->d = n-1; $bCtr = ($this->b)/2; $dCtr = ($this->d)/2;; $this->mx = $mx; } public function rotate($r) { $k = 0; while(1) { $this->a += $k; $this->b -= $k; $this->c += $k; $this->d -= $k; if ( $this->a === $this->b || $this->c === $this->d ) { break; } $mult = 2*($this->m-2*$k)+2*($this->n-2*$k)-4; $rm = $r % $mult; while($rm--) { rotateSquare(); } ++$k; } } private function rotateSquare($k) { $t = []; //transite for($i=0; $i<$this->m; ++$i) { for($j=0; $j<$this->n; ++$j) { $t[$i][$j] = [0,0]; } } $j=$this->c; for($i=$this->a; $i<$this->b; ++$i) { $t[$i][$j] = [-1,0]; } $i=$this->b; for($j=$this->c; $j<$this->d; ++$j) { $t[$i][$j] = [0,1]; } $j=$this->d; for($i=$this->b; $i>$this->a; --$i) { $t[$i][$j] = [1,0]; } $i=$this->a; for($j=$this->d; $j<$this->c; --$j) { $t[$i][$j] = [0,-1]; } $temp = []; for($i=0; $i<$this->m; ++$i) { for($j=0; $j<$this->n; ++$j) { $temp[$i + $t[$i][$j][0] ] [$j + $t[$i][$j][1] ] = $this->mx[$i][$j]; } } $this->mx = $this->ksort($temp); return $this; } private function ksort($temp) { foreach($temp as &$ar){ ksort($ar); } ksort($temp); return $temp; } }

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.70.0090.00616.63
8.3.60.0150.00018.18
8.3.50.0090.00616.50
8.3.40.0110.01118.71
8.3.30.0090.00618.61
8.3.20.0060.00324.18
8.3.10.0080.00024.66
8.3.00.0000.00826.16
8.2.190.0110.00416.63
8.2.180.0040.01125.92
8.2.170.0150.00018.96
8.2.160.0100.00722.96
8.2.150.0000.00825.66
8.2.140.0040.00424.66
8.2.130.0070.00026.16
8.2.120.0040.00426.35
8.2.110.0060.00320.50
8.2.100.0060.00617.78
8.2.90.0060.00317.88
8.2.80.0080.00018.77
8.2.70.0090.00017.50
8.2.60.0000.00817.38
8.2.50.0080.00018.05
8.2.40.0040.00419.82
8.2.30.0040.00418.20
8.2.20.0070.00017.95
8.2.10.0050.00219.38
8.2.00.0040.00419.39
8.1.280.0090.00625.92
8.1.270.0040.00423.99
8.1.260.0080.00028.09
8.1.250.0040.00428.09
8.1.240.0000.01119.32
8.1.230.0090.00322.65
8.1.220.0030.00618.59
8.1.210.0040.00418.77
8.1.200.0060.00317.41
8.1.190.0090.00017.25
8.1.180.0000.00818.10
8.1.170.0050.00318.69
8.1.160.0040.00418.77
8.1.150.0070.00018.91
8.1.140.0040.00418.82
8.1.130.0040.00420.16
8.1.120.0040.00417.42
8.1.110.0050.00317.44
8.1.100.0080.00017.45
8.1.90.0070.00017.45
8.1.80.0030.00617.38
8.1.70.0030.00317.53
8.1.60.0040.00417.49
8.1.50.0050.00317.43
8.1.40.0040.00417.55
8.1.30.0040.00417.55
8.1.20.0030.00617.62
8.1.10.0040.00417.59
8.1.00.0050.00317.49
8.0.300.0040.00418.77
8.0.290.0000.00816.75
8.0.280.0000.00818.38
8.0.270.0030.00317.97
8.0.260.0030.00320.41
8.0.250.0040.00416.97
8.0.240.0000.00816.86
8.0.230.0030.00317.04
8.0.220.0030.00316.94
8.0.210.0030.00316.94
8.0.200.0070.00016.86
8.0.190.0040.00416.84
8.0.180.0040.00416.91
8.0.170.0100.00016.83
8.0.160.0030.00616.92
8.0.150.0000.00716.82
8.0.140.0000.00716.79
8.0.130.0030.00313.35
8.0.120.0040.00416.94
8.0.110.0000.00716.73
8.0.100.0040.00416.77
8.0.90.0000.00716.92
8.0.80.0130.00316.87
8.0.70.0050.00316.74
8.0.60.0030.00516.98
8.0.50.0040.00416.87
8.0.30.0070.01317.07
8.0.20.0060.01317.09
8.0.10.0000.00817.03
8.0.00.0110.01116.74
7.4.330.0050.00015.55
7.4.320.0040.00416.56
7.4.300.0060.00016.55
7.4.290.0050.00316.55
7.4.280.0040.00416.43
7.4.270.0060.00316.68
7.4.260.0050.00013.34
7.4.250.0030.00616.64
7.4.240.0080.00016.66
7.4.230.0040.00416.64
7.4.220.0040.00416.50
7.4.210.0040.01216.56
7.4.200.0000.00716.38
7.4.130.0080.01216.57
7.4.120.0130.00316.57
7.4.110.0100.00716.38
7.4.100.0100.00716.65
7.4.90.0100.00616.63
7.4.80.0070.01016.65
7.4.70.0060.01216.25
7.4.60.0060.01016.51
7.4.50.0140.00316.33
7.4.40.0090.00916.41
7.4.30.0170.00016.36
7.4.20.0110.00716.57
7.4.10.0070.01116.36
7.4.00.0090.00616.20
7.3.330.0000.00716.32
7.3.320.0000.00513.21
7.3.310.0040.00416.12
7.3.300.0030.00316.19
7.3.290.0080.01016.24
7.3.280.0030.01516.25
7.3.260.0130.00716.21
7.3.230.0150.00316.25
7.3.210.0120.00916.14
7.3.200.0100.00716.15
7.3.190.0070.01016.29
7.3.180.0080.00816.55
7.3.170.0080.00816.28
7.3.160.0070.01016.59
7.3.150.0070.01016.13
7.3.140.0040.01216.30
7.3.130.0100.00716.15
7.3.120.0160.00016.19
7.3.110.0120.00416.18
7.3.100.0100.00716.25
7.3.90.0030.01316.39
7.3.80.0120.00316.29
7.3.70.0040.01216.21
7.3.60.0100.00716.27
7.3.50.0070.01016.23
7.3.40.0090.00616.29
7.3.30.0090.00616.13
7.3.20.0000.02316.30
7.3.10.0150.00316.13
7.3.00.0100.00716.46
7.2.330.0090.00916.55
7.2.320.0070.01016.55
7.2.310.0060.00916.50
7.2.300.0080.00816.59
7.2.290.0100.00716.53
7.2.280.0130.00316.36
7.2.270.0140.00316.48
7.2.260.0130.00316.25
7.2.250.0060.01016.52
7.2.240.0060.01116.45
7.2.230.0090.00916.57
7.2.220.0060.01016.55
7.2.210.0110.00616.38
7.2.200.0130.00316.29
7.2.190.0100.00716.41
7.2.180.0040.01416.39
7.2.170.0040.01216.37
7.2.160.0040.01216.41
7.2.150.0070.01116.49
7.2.140.0110.00716.47
7.2.130.0040.01416.49
7.2.120.0120.00616.49
7.2.110.0170.00016.66
7.2.100.0030.01416.46
7.2.90.0100.00716.35
7.2.80.0130.01016.33
7.2.70.0090.00916.61
7.2.60.0140.00316.48
7.2.50.0030.01316.27
7.2.40.0160.00416.27
7.2.30.0090.00916.55
7.2.20.0000.01616.43
7.2.10.0130.00716.51
7.2.00.0090.01616.49

preferences:
49.97 ms | 401 KiB | 5 Q