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(); print_r($m->get()); } 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.60.0090.00618.18
8.3.50.0150.00316.51
8.3.40.0120.00318.59
8.3.30.0090.00918.52
8.3.20.0090.00024.18
8.3.10.0000.00824.66
8.3.00.0030.00626.16
8.2.180.0110.00425.92
8.2.170.0040.01118.91
8.2.160.0130.00322.96
8.2.150.0040.00425.66
8.2.140.0050.00324.66
8.2.130.0050.00326.16
8.2.120.0040.00426.35
8.2.110.0000.01020.39
8.2.100.0080.00317.78
8.2.90.0030.00517.48
8.2.80.0000.00818.61
8.2.70.0030.00617.63
8.2.60.0030.00517.63
8.2.50.0050.00318.05
8.2.40.0040.00419.85
8.2.30.0050.00517.95
8.2.20.0000.00817.95
8.2.10.0040.00419.13
8.2.00.0050.00219.18
8.1.280.0040.01425.92
8.1.270.0050.00323.99
8.1.260.0060.00328.09
8.1.250.0040.00428.09
8.1.240.0040.00719.28
8.1.230.0060.00622.74
8.1.220.0000.00818.56
8.1.210.0030.00618.77
8.1.200.0060.00317.53
8.1.190.0040.00417.23
8.1.180.0040.00418.10
8.1.170.0000.00818.70
8.1.160.0040.00418.77
8.1.150.0000.00718.93
8.1.140.0070.00018.90
8.1.130.0030.00320.20
8.1.120.0080.00017.36
8.1.110.0040.00417.44
8.1.100.0050.00317.48
8.1.90.0040.00417.36
8.1.80.0040.00717.45
8.1.70.0040.00417.43
8.1.60.0040.00417.52
8.1.50.0060.00317.43
8.1.40.0040.00417.51
8.1.30.0040.00417.60
8.1.20.0040.00417.55
8.1.10.0040.00417.52
8.1.00.0030.00517.43
8.0.300.0000.00818.77
8.0.290.0000.00816.75
8.0.280.0040.00418.52
8.0.270.0040.00417.91
8.0.260.0030.00320.20
8.0.250.0080.00016.96
8.0.240.0050.00317.01
8.0.230.0070.00017.03
8.0.220.0080.00016.89
8.0.210.0040.00416.89
8.0.200.0000.00717.00
8.0.190.0040.00416.99
8.0.180.0070.00016.96
8.0.170.0000.00816.81
8.0.160.0040.00416.88
8.0.150.0000.00716.87
8.0.140.0080.00016.79
8.0.130.0000.00513.37
8.0.120.0050.00316.86
8.0.110.0040.00416.80
8.0.100.0040.00416.92
8.0.90.0000.00716.87
8.0.80.0040.01316.87
8.0.70.0000.00716.68
8.0.60.0050.00316.91
8.0.50.0000.00716.73
8.0.30.0070.01017.12
8.0.20.0150.00717.07
8.0.10.0030.00516.98
8.0.00.0150.01216.73
7.4.330.0030.00315.55
7.4.320.0030.00316.64
7.4.300.0000.00616.61
7.4.290.0070.00016.54
7.4.280.0050.00316.45
7.4.270.0070.00316.43
7.4.260.0030.00313.29
7.4.250.0030.00316.48
7.4.240.0040.00416.50
7.4.230.0040.00416.45
7.4.220.0020.00516.71
7.4.210.0050.00916.55
7.4.200.0030.00316.62
7.4.130.0080.01316.46
7.4.120.0070.01116.40
7.4.110.0070.01116.54
7.4.100.0060.01016.56
7.4.90.0310.00616.50
7.4.80.0140.01016.17
7.4.70.0180.00716.23
7.4.60.0140.01116.60
7.4.50.0260.00316.49
7.4.40.0240.00816.29
7.4.30.0130.01716.54
7.4.20.0250.00616.25
7.4.10.0190.01216.54
7.4.00.0090.00916.12
7.3.330.0040.00416.21
7.3.320.0000.00513.26
7.3.310.0000.00716.07
7.3.300.0040.00416.27
7.3.290.0090.00716.25
7.3.280.0060.00916.18
7.3.260.0120.01216.43
7.3.230.0040.01316.43
7.3.210.0260.00916.02
7.3.200.0110.01516.10
7.3.190.0200.00516.39
7.3.180.0260.00316.23
7.3.170.0300.00316.18
7.3.160.0240.00816.38
7.3.150.0270.00316.25
7.3.140.0260.00916.22
7.3.130.0300.00516.23
7.3.120.0210.01116.28
7.3.110.0250.00416.40
7.3.100.0180.01416.47
7.3.90.0200.01116.35
7.3.80.0230.01016.21
7.3.70.0190.01216.18
7.3.60.0210.00816.39
7.3.50.0260.01116.41
7.3.40.0270.00316.33
7.3.30.0240.00716.34
7.3.20.0250.00616.39
7.3.10.0180.01516.21
7.3.00.0530.00816.34
7.2.330.0230.00816.27
7.2.320.0190.01016.61
7.2.310.0150.01516.60
7.2.300.0210.00916.31
7.2.290.0200.01016.61
7.2.280.0270.00316.61
7.2.270.0230.00716.62
7.2.260.0270.00416.47
7.2.250.0180.01516.63
7.2.240.0230.00916.50
7.2.230.0190.01116.37
7.2.220.0220.01016.59
7.2.210.0200.01016.47
7.2.200.0280.00416.38
7.2.190.0300.00716.42
7.2.180.0190.02016.39
7.2.170.0230.00916.48
7.2.160.0210.01116.23
7.2.150.0180.01416.57
7.2.140.0250.00816.45
7.2.130.0160.01616.39
7.2.120.0380.00016.39
7.2.110.0280.00716.56
7.2.100.0240.00916.62
7.2.90.0280.00816.49
7.2.80.0220.01516.51
7.2.70.0230.01116.45
7.2.60.0230.01316.64
7.2.50.0170.01716.50
7.2.40.0220.01116.43
7.2.30.0200.01116.65
7.2.20.0220.01216.41
7.2.10.0290.00416.59
7.2.00.0240.00716.50

preferences:
37.48 ms | 400 KiB | 5 Q