3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { public $roots = []; public function addCategory($category, $parent) { if ($this->categoryExists($this->roots, $category)) { throw new InvalidArgumentException("Category: $category already exists."); } if ($parent === null) { $this->roots[$category] = []; return; } $node = &$this->findNode($parent, $this->roots); if(!isset($node)) { throw new InvalidArgumentException("Parent node: $parent not found."); } $node[$category] = []; } public function getChildren($parent) { $node = $this->findNode($parent, $this->roots); if(!isset($node)) { throw new InvalidArgumentException("Parent node: $parent not found."); } return array_keys($node); } protected function &findNode($node, &$root) { if (in_array($node, array_keys($root))) { return $root[$node]; } foreach ($root as $n) { return $this->findNode($node, $n); } } public function categoryExists($nodes, $category) { $exists = false; foreach($nodes as $key => $node) { if (in_array($category, $this->getChildren($key))) { return true; } $exists = $this->categoryExists($node, $category); } return $exists; } } // For testing purposes (do not submit uncommented): $c = new CategoryTree; $c->addCategory('A', null); $c->addCategory('B', 'A'); $c->addCategory('C', 'A'); echo implode(',', $c->getChildren('A')); echo "\n". $c->categoryExists($c->roots, 'A');

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.0100.01016.88
8.3.50.0240.00822.18
8.3.40.0060.00918.65
8.3.30.0000.01418.70
8.3.20.0080.00020.33
8.3.10.0000.00823.66
8.3.00.0080.00019.87
8.2.180.0110.01116.63
8.2.170.0120.00922.96
8.2.160.0170.00320.37
8.2.150.0080.00024.18
8.2.140.0030.00624.66
8.2.130.0090.00021.04
8.2.120.0040.00426.35
8.2.110.0060.00320.51
8.2.100.0070.00418.15
8.2.90.0000.00919.17
8.2.80.0060.00317.97
8.2.70.0060.00317.50
8.2.60.0040.00818.04
8.2.50.0060.00318.07
8.2.40.0050.00919.01
8.2.30.0080.00020.71
8.2.20.0050.00317.88
8.2.10.0080.00018.19
8.2.00.0000.00818.34
8.1.280.0090.00625.92
8.1.270.0090.00020.20
8.1.260.0040.00428.09
8.1.250.0050.00328.09
8.1.240.0040.00423.92
8.1.230.0110.00017.70
8.1.220.0000.00917.74
8.1.210.0000.00818.77
8.1.200.0100.00017.35
8.1.190.0040.00417.25
8.1.180.0060.00318.10
8.1.170.0000.00818.64
8.1.160.0040.00420.80
8.1.150.0050.00318.96
8.1.140.0100.00019.66
8.1.130.0030.00317.66
8.1.120.0070.00017.52
8.1.110.0080.00017.54
8.1.100.0040.00417.53
8.1.90.0040.00417.56
8.1.80.0070.00017.52
8.1.70.0000.00717.38
8.1.60.0040.00417.54
8.1.50.0000.00817.56
8.1.40.0030.00617.62
8.1.30.0000.00817.61
8.1.20.0060.00317.60
8.1.10.0000.00817.58
8.1.00.0040.00417.43
8.0.300.0000.00818.77
8.0.290.0000.00716.88
8.0.280.0040.00418.43
8.0.270.0030.00317.39
8.0.260.0000.00617.31
8.0.250.0000.00716.90
8.0.240.0000.00616.98
8.0.230.0000.00716.84
8.0.220.0000.00816.96
8.0.210.0000.00816.95
8.0.200.0000.00617.02
8.0.190.0080.00016.98
8.0.180.0090.00016.85
8.0.170.0070.00016.87
8.0.160.0040.00416.84
8.0.150.0060.00316.84
8.0.140.0030.00716.85
8.0.130.0000.00513.32
8.0.120.0050.00316.83
8.0.110.0030.00516.86
8.0.100.0000.00716.80
8.0.90.0050.00216.99
8.0.80.0060.01316.89
8.0.70.0040.00416.74
8.0.60.0040.00416.89
8.0.50.0020.00516.84
8.0.30.0080.00817.15
8.0.20.0100.00917.40
8.0.10.0080.00017.13
8.0.00.0110.00716.90
7.4.330.0050.00016.77
7.4.320.0030.00316.62
7.4.300.0000.00616.70
7.4.290.0040.00416.62
7.4.280.0050.00316.54
7.4.270.0070.00016.55
7.4.260.0030.00316.45
7.4.250.0020.00516.66
7.4.240.0000.00716.56
7.4.230.0030.00316.54
7.4.220.0180.00616.68
7.4.210.0070.00716.49
7.4.200.0040.00416.47
7.4.160.0110.00416.50
7.4.150.0040.01317.40
7.4.140.0150.01017.86
7.4.130.0090.01116.68
7.4.120.0120.00816.50
7.4.110.0100.00716.56
7.4.100.0090.01316.46
7.4.90.0070.01016.57
7.4.80.0070.01419.39
7.4.70.0110.00616.57
7.4.60.0090.00616.57
7.4.50.0040.01116.50
7.4.40.0080.00816.34
7.4.30.0110.01116.53
7.4.00.0030.01414.96
7.3.330.0060.00013.42
7.3.320.0000.00813.43
7.3.310.0000.00716.45
7.3.300.0040.00416.33
7.3.290.0110.00616.41
7.3.280.0070.01016.44
7.3.270.0130.00417.40
7.3.260.0090.00916.48
7.3.250.0090.01316.44
7.3.240.0110.00916.41
7.3.230.0100.01016.43
7.3.210.0100.00716.71
7.3.200.0090.00916.52
7.3.190.0070.01016.49
7.3.180.0110.00616.33
7.3.170.0060.01516.47
7.3.160.0100.00716.70
7.3.120.0060.00914.86
7.3.110.0090.00814.75
7.3.100.0000.01515.18
7.3.90.0100.00314.96
7.3.80.0030.01115.09
7.3.70.0090.00815.07
7.3.60.0110.00314.81
7.3.50.0100.00715.09
7.3.40.0060.00614.64
7.3.30.0000.01614.80
7.3.20.0030.01216.73
7.3.10.0000.01016.69
7.3.00.0030.01016.80
7.2.330.0130.01016.53
7.2.320.0080.00916.90
7.2.310.0080.00816.87
7.2.300.0130.00916.75
7.2.290.0120.00616.85
7.2.250.0100.01015.13
7.2.240.0100.00715.14
7.2.230.0060.00915.20
7.2.220.0100.00714.96
7.2.210.0040.01415.00
7.2.200.0040.01515.30
7.2.190.0090.00615.12
7.2.180.0060.00915.16
7.2.170.0110.00315.18
7.2.00.0040.00819.36
7.1.330.0130.00015.99
7.1.320.0040.00415.90
7.1.310.0000.01615.88
7.1.300.0070.01016.02
7.1.290.0060.00615.66
7.1.280.0030.01015.81
7.1.270.0100.00715.91
7.1.260.0000.01115.85
7.1.100.0230.00017.99
7.1.70.0070.00417.34
7.1.60.0110.01519.13
7.1.50.0100.01016.97
7.1.00.0030.07722.33
7.0.200.0000.00816.83
7.0.60.0170.06321.73
7.0.50.0030.04717.90
7.0.40.0070.04320.16
7.0.30.0300.06720.21
7.0.20.0330.08020.07
7.0.10.0100.08720.22
7.0.00.0200.08020.23
5.6.210.0130.07720.57
5.6.200.0130.08018.24
5.6.190.0170.03720.57
5.6.180.0230.06020.46
5.6.170.0270.07720.52
5.6.160.0130.07320.47
5.6.150.0000.04318.21
5.6.140.0130.07318.27
5.6.130.0230.07018.18
5.6.120.0100.08721.16
5.6.110.0130.07720.90
5.6.100.0100.06321.04
5.6.90.0100.08320.98
5.6.80.0030.06320.43
5.5.350.0270.03320.45
5.5.340.0070.03318.07
5.5.330.0070.08320.19
5.5.320.0470.05020.37
5.5.310.0130.08320.37
5.5.300.0030.06718.07
5.5.290.0030.05317.93
5.5.280.0030.04320.90
5.5.270.0000.04320.97
5.5.260.0030.04320.69
5.5.250.0030.08720.68
5.5.240.0200.07320.38

preferences:
42.51 ms | 401 KiB | 5 Q