3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = 'RA(B(CCC)B(CC))PA(BB(C))D'; abstract class Component { public $letter; public function __construct($letter) { $this->letter = $letter; } public function dump($level = 0) { echo str_repeat("&emsp;", $level).$this->letter."<br>"; } } class Leaf extends Component {} class Node extends Component { /** @var Component[] */ private $items = []; private static function findComponentsFromInput($input) { $numOpenBrackets = false; $numClosedBrackets = false; $trimPositions = []; $lastTrimPosition = 0; for ($currentCharNumber = 0, $strLen = strlen($input); $currentCharNumber < $strLen; $currentCharNumber++) { if ($input{$currentCharNumber} == '(') { $numOpenBrackets = (int)$numOpenBrackets + 1; } elseif ($input{$currentCharNumber} == ')') { $numClosedBrackets = (int)$numClosedBrackets + 1; } $isLeaf = $numOpenBrackets === false && $numClosedBrackets === false && $input{$currentCharNumber + 1} != '('; if ($isLeaf) { $trimPositions[] = [ "type" => 'leaf', "start_trim" => $lastTrimPosition, "trim_length" => $currentCharNumber - $lastTrimPosition + 1 ]; $lastTrimPosition = $currentCharNumber + 1; } $isNode = is_int($numOpenBrackets) && is_int($numClosedBrackets) && $numOpenBrackets - $numClosedBrackets == 0; if ($isNode) { $numOpenBrackets = false; $numClosedBrackets = false; $trimPositions[] = [ "type" => "node", "start_trim" => $lastTrimPosition, "trim_length" => $currentCharNumber - $lastTrimPosition + 1 ]; $lastTrimPosition = $currentCharNumber + 1; } } $resultComponents = []; foreach ($trimPositions as $trimPosition) { $resultComponents[] = [ "type" => $trimPosition["type"], "data" => substr($input, $trimPosition["start_trim"], $trimPosition["trim_length"]) ]; } return $resultComponents; } public function createComponentsFromInput($dataString) { $components = self::findComponentsFromInput($dataString); foreach($components as $component) { if($component['type'] == 'leaf') { $this->addItem(new Leaf($component['data'])); } elseif($component['type'] == 'node') { preg_match('~(\w)*\((.+)\)~', $component['data'], $matches); if(!isset($matches[1]) || !isset($matches[2])) { echo "Wrong input format"; exit; } $newNode = new Node($matches[1]); $newNode->createComponentsFromInput($matches[2]); $this->addItem($newNode); } } return $this; } public function addItem(Component $item) { $this->items[] = $item; } public function dump($level = 0) { if($this->letter == 'root') { $newLevel = $level; } else { parent::dump($level); $newLevel = $level + 1; } foreach($this->items as $item) { $item->dump($newLevel); } return $this; } } $root = new Node("root"); $root->createComponentsFromInput($input)->dump();

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.01418.30
8.3.50.0130.00718.07
8.3.40.0110.00418.67
8.3.30.0140.00018.69
8.3.20.0000.00820.09
8.3.10.0040.00423.64
8.3.00.0070.00319.68
8.2.180.0120.00618.38
8.2.170.0070.00722.96
8.2.160.0120.00319.14
8.2.150.0110.00424.18
8.2.140.0080.00024.66
8.2.130.0080.00026.16
8.2.120.0040.00418.77
8.2.110.0030.00622.12
8.2.100.0060.00617.59
8.2.90.0040.00419.14
8.2.80.0040.00417.97
8.2.70.0000.00817.37
8.2.60.0050.00317.91
8.2.50.0040.00418.10
8.2.40.0090.00022.27
8.2.30.0040.00419.07
8.2.20.0000.00717.87
8.2.10.0000.00717.96
8.2.00.0040.00417.86
8.1.280.0120.00625.92
8.1.270.0000.00823.97
8.1.260.0040.00426.35
8.1.250.0000.00728.09
8.1.240.0050.00520.95
8.1.230.0080.00319.07
8.1.220.0070.00317.74
8.1.210.0030.00518.77
8.1.200.0000.01017.13
8.1.190.0060.00317.22
8.1.180.0000.00818.10
8.1.170.0030.00618.55
8.1.160.0040.00418.84
8.1.150.0070.00318.82
8.1.140.0050.00319.42
8.1.130.0030.00518.69
8.1.120.0040.00417.27
8.1.110.0050.00217.20
8.1.100.0040.00417.34
8.1.90.0040.00417.33
8.1.80.0000.00717.24
8.1.70.0000.00717.34
8.1.60.0030.00617.53
8.1.50.0070.00317.41
8.1.40.0000.00717.41
8.1.30.0030.00617.55
8.1.20.0000.01017.58
8.1.10.0000.00817.45
8.1.00.0050.00517.34
8.0.300.0080.00018.77
8.0.290.0070.00016.63
8.0.280.0000.00718.33
8.0.270.0040.00417.16
8.0.260.0030.00318.71
8.0.250.0000.00616.93
8.0.240.0060.00316.91
8.0.230.0000.00716.78
8.0.220.0000.00716.82
8.0.210.0030.00316.73
8.0.200.0030.00316.89
8.0.190.0060.00316.91
8.0.180.0040.00416.76
8.0.170.0040.00416.82
8.0.160.0000.00816.73
8.0.150.0070.00316.63
8.0.140.0070.00016.65
8.0.130.0030.00313.27
8.0.120.0040.00416.77
8.0.110.0070.00016.66
8.0.100.0000.00816.71
8.0.90.0040.00416.66
8.0.80.0100.00716.89
8.0.70.0000.00816.64
8.0.60.0040.00416.71
8.0.50.0040.00416.89
8.0.30.0090.01016.89
8.0.20.0120.00817.40
8.0.10.0000.00716.85
8.0.00.0130.00316.60
7.4.330.0050.00015.55
7.4.320.0060.00016.87
7.4.300.0000.00716.75
7.4.290.0030.00316.90
7.4.280.0000.00716.67
7.4.270.0000.00716.78
7.4.260.0080.00016.77
7.4.250.0040.00416.70
7.4.240.0040.00416.77
7.4.230.0080.00016.86
7.4.220.0070.01716.77
7.4.210.0070.01316.75
7.4.200.0070.00016.80
7.4.160.0060.01416.82
7.4.150.0060.01317.40
7.4.140.0070.01217.86
7.4.130.0060.01316.66
7.4.120.0120.00916.77
7.4.110.0100.01016.75
7.4.100.0160.00316.77
7.4.90.0140.00316.80
7.4.80.0180.00319.39
7.4.70.0040.01416.90
7.4.60.0100.00716.69
7.4.50.0120.00316.57
7.4.40.0110.00816.86
7.4.30.0080.00816.83
7.4.00.0080.00815.14
7.3.330.0000.00513.61
7.3.320.0050.00013.47
7.3.310.0000.00716.63
7.3.300.0040.00416.61
7.3.290.0080.00916.59
7.3.280.0090.00916.59
7.3.270.0090.00917.40
7.3.260.0070.01116.75
7.3.250.0140.00916.70
7.3.240.0090.00816.64
7.3.230.0330.02316.83
7.3.210.0100.00716.84
7.3.200.0080.01016.64
7.3.190.0070.01016.70
7.3.180.0120.00616.72
7.3.170.0060.01016.63
7.3.160.0090.00616.91
7.3.120.0000.01416.90
7.3.110.0060.00916.90
7.3.100.0070.01116.90
7.3.90.0040.01116.90
7.3.80.0000.01416.90
7.3.70.0040.00816.90
7.3.60.0120.00016.90
7.3.50.0160.00016.90
7.3.40.0030.01016.90
7.3.30.0030.00716.90
7.3.20.0070.00716.90
7.3.10.0030.01216.90
7.3.00.0030.01316.90
7.2.330.0070.01117.04
7.2.320.0030.01516.84
7.2.310.0110.00716.90
7.2.300.0040.01317.06
7.2.290.0120.00716.80
7.2.240.0030.01316.90
7.2.230.0000.01716.90
7.2.220.0000.01516.90
7.2.210.0030.01316.90
7.2.200.0100.00316.90
7.2.190.0110.00416.90
7.2.180.0090.00616.90
7.2.170.0100.00716.90
7.2.160.0040.00816.90
7.2.150.0030.01416.90
7.2.140.0030.00617.22
7.2.130.0060.00917.22
7.2.120.0060.01016.91
7.2.110.0120.00816.84
7.2.100.0070.00717.16
7.2.90.0030.01317.18
7.2.80.0060.00617.30
7.2.70.0090.00617.23
7.2.60.0060.00817.05
7.2.50.0070.00317.36
7.2.40.0090.00617.05
7.2.30.0060.00617.14
7.2.20.0000.01217.13
7.2.10.0000.01717.18
7.2.00.0030.00617.30
7.1.330.0060.00916.90
7.1.320.0040.00716.90
7.1.310.0000.01416.90
7.1.300.0070.01016.90
7.1.290.0130.00016.90
7.1.280.0000.01316.90
7.1.270.0070.00716.90
7.1.260.0040.01416.90
7.1.250.0030.01016.90
7.1.200.0130.00615.82
7.1.70.0640.01115.55
7.1.60.0820.02633.35
7.1.50.1000.01033.18
7.1.40.0910.01333.18
7.1.30.0650.01432.86
7.1.20.0530.02033.16
7.1.10.0800.01015.00
7.1.00.0670.00615.22
7.0.200.0610.01314.93
7.0.190.0670.01715.34
7.0.180.0740.00714.81
7.0.170.0560.00914.52
7.0.160.0680.01414.93
7.0.150.0700.01314.92
7.0.140.2110.00614.83
7.0.130.0500.01014.76
7.0.120.0660.01115.08
7.0.110.1670.01614.91
7.0.100.0710.01114.86
7.0.90.0890.01414.88
7.0.80.0760.00914.77
7.0.70.0740.02214.72
7.0.60.0520.01014.70
7.0.50.0810.01014.89
7.0.40.0530.00615.00
7.0.30.0470.00714.71
7.0.20.0690.00615.05
7.0.10.0610.00914.83
7.0.00.0590.00714.85

preferences:
55.18 ms | 400 KiB | 5 Q