3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { protected $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); } } protected function categoryExists($nodes, $category) { $exists = false; foreach($nodes as $key => $node) { if ($key === $category) { 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('A', 'B'); $c->addCategory('C', 'A'); echo implode(',', $c->getChildren('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.0120.00316.88
8.3.50.0110.00821.98
8.3.40.0140.00318.96
8.3.30.0140.00418.66
8.3.20.0060.00320.24
8.3.10.0080.00023.64
8.3.00.0080.00019.97
8.2.180.0040.01118.29
8.2.170.0070.00722.96
8.2.160.0030.01720.54
8.2.150.0030.00524.18
8.2.140.0060.00324.66
8.2.130.0000.00826.16
8.2.120.0000.00721.06
8.2.110.0070.00320.97
8.2.100.0080.00317.93
8.2.90.0040.00419.26
8.2.80.0050.00317.97
8.2.70.0040.00417.75
8.2.60.0040.00417.93
8.2.50.0030.00518.07
8.2.40.0000.00820.98
8.2.30.0000.00820.70
8.2.20.0060.00317.89
8.2.10.0050.00318.27
8.2.00.0080.00018.13
8.1.280.0090.00625.92
8.1.270.0060.00319.00
8.1.260.0040.00426.35
8.1.250.0000.00828.09
8.1.240.0070.00320.87
8.1.230.0030.00917.89
8.1.220.0030.00617.74
8.1.210.0000.00818.77
8.1.200.0070.00317.38
8.1.190.0030.00617.36
8.1.180.0000.00918.10
8.1.170.0030.00618.47
8.1.160.0000.00720.82
8.1.150.0000.00718.93
8.1.140.0060.00319.62
8.1.130.0000.00717.68
8.1.120.0040.00417.48
8.1.110.0050.00217.53
8.1.100.0030.00617.54
8.1.90.0070.00317.43
8.1.80.0000.00817.41
8.1.70.0000.00717.39
8.1.60.0040.00417.63
8.1.50.0070.00317.46
8.1.40.0040.00417.58
8.1.30.0040.00417.59
8.1.20.0060.00317.67
8.1.10.0000.00817.63
8.1.00.0050.00317.53
8.0.300.0040.00418.77
8.0.290.0040.00416.88
8.0.280.0000.00718.55
8.0.270.0030.00517.18
8.0.260.0000.00617.30
8.0.250.0000.00716.96
8.0.240.0030.00617.05
8.0.230.0040.00417.07
8.0.220.0040.00416.99
8.0.210.0000.00717.00
8.0.200.0060.00017.00
8.0.190.0040.00417.02
8.0.180.0050.00316.84
8.0.170.0000.00716.86
8.0.160.0000.00717.02
8.0.150.0040.00416.94
8.0.140.0100.00016.93
8.0.130.0000.00513.46
8.0.120.0000.00916.98
8.0.110.0050.00217.04
8.0.100.0040.00416.96
8.0.90.0070.00017.02
8.0.80.0040.01216.95
8.0.70.0020.00516.80
8.0.60.0030.00517.03
8.0.50.0050.00316.98
8.0.30.0070.01317.21
8.0.20.0090.00917.40
8.0.10.0070.00016.95
8.0.00.0080.01016.87
7.4.330.0030.00316.79
7.4.320.0070.00016.56
7.4.300.0000.00716.55
7.4.290.0050.00316.57
7.4.280.0030.00716.63
7.4.270.0000.00716.48
7.4.260.0030.00516.64
7.4.250.0050.00216.57
7.4.240.0030.00516.57
7.4.230.0000.00716.63
7.4.220.0130.00616.58
7.4.210.0080.00816.60
7.4.200.0040.00416.43
7.4.160.0120.01216.43
7.4.150.0140.00317.40
7.4.140.0120.01217.86
7.4.130.0110.00616.60
7.4.120.0160.00516.65
7.4.110.0120.00616.64
7.4.100.0090.01216.57
7.4.90.0110.01116.45
7.4.80.0120.01019.39
7.4.70.0070.01016.73
7.4.60.0090.00616.50
7.4.50.0000.01616.46
7.4.40.0090.00716.69
7.4.30.0100.00716.45
7.4.00.0030.00715.27
7.3.330.0060.00013.48
7.3.320.0030.00313.45
7.3.310.0030.00316.35
7.3.300.0030.00316.49
7.3.290.0030.00316.52
7.3.280.0110.00716.46
7.3.270.0130.00817.40
7.3.260.0070.01116.43
7.3.250.0080.01216.55
7.3.240.0100.00616.64
7.3.230.0110.00716.67
7.3.210.0030.01316.55
7.3.200.0070.01016.74
7.3.190.0160.00316.69
7.3.180.0060.01116.70
7.3.170.0030.01516.68
7.3.160.0070.01016.46
7.2.330.0120.00616.77
7.2.320.0030.01616.62
7.2.310.0030.01316.57
7.2.300.0110.00716.71
7.2.290.0100.01016.80
7.2.60.0030.01316.88
7.2.00.0030.01219.58
7.1.200.0090.00615.80
7.1.100.0070.00717.90
7.1.70.0070.00017.26
7.1.60.0110.01419.13
7.1.50.0030.01917.11
7.1.00.0070.07322.48
7.0.200.0030.00616.64
7.0.60.0130.07319.97
7.0.50.0070.08017.85
7.0.40.0130.08020.10
7.0.30.0170.04720.23
7.0.20.0200.08020.06
7.0.10.0000.08020.06
7.0.00.0070.08320.18
5.6.280.0000.03321.05
5.6.210.0000.04320.52
5.6.200.0130.08318.24
5.6.190.0030.09320.64
5.6.180.0270.06020.47
5.6.170.0100.05320.50
5.6.160.0070.08720.63
5.6.150.0070.05318.29
5.6.140.0030.07318.28
5.6.130.0070.07718.24
5.6.120.0030.04320.91
5.6.110.0130.07320.99
5.6.100.0030.04321.00
5.6.90.0100.08021.08
5.6.80.0070.07720.42
5.5.350.2430.03720.42
5.5.340.0130.07018.10
5.5.330.0100.04720.57
5.5.320.0300.08020.24
5.5.310.0070.05020.19
5.5.300.0000.07017.98
5.5.290.0070.07718.02
5.5.280.0100.06020.86
5.5.270.0000.04720.80
5.5.260.0130.06320.81
5.5.250.0000.06020.63
5.5.240.0070.03720.29

preferences:
47.74 ms | 401 KiB | 5 Q