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(); } if ($parent === null) { $this->tree[$category] = []; return; } $node = $this->findNode($parent, $this->roots); if(!isset($node)) { throw new InvalidArgumentException(); } $node[$category] = []; } public function getChildren($parent) { $node = $this->findNode($parent, $this->roots); if(!isset($node)) { throw new InvalidArgumentException(); } return array_keys($node); } public 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 $node) { if (in_array($category, $this->getChildren($node))) { 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'));

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.0110.00416.75
8.3.50.0150.00318.43
8.3.40.0120.00319.09
8.3.30.0110.00418.80
8.3.20.0000.00820.34
8.3.10.0080.00023.69
8.3.00.0050.00319.91
8.2.180.0070.01418.29
8.2.170.0140.00719.15
8.2.160.0110.00322.96
8.2.150.0170.00025.66
8.2.140.0080.00024.66
8.2.130.0040.00421.09
8.2.120.0050.00326.35
8.2.110.0060.00321.05
8.2.100.0120.00017.93
8.2.90.0050.00319.40
8.2.80.0000.00917.97
8.2.70.0080.00317.49
8.2.60.0070.00317.93
8.2.50.0030.00518.07
8.2.40.0040.00421.19
8.2.30.0000.00820.69
8.2.20.0080.00017.98
8.2.10.0000.00818.13
8.2.00.0000.00718.35
8.1.280.0040.01425.92
8.1.270.0060.00318.99
8.1.260.0080.00028.09
8.1.250.0080.00028.09
8.1.240.0060.00322.65
8.1.230.0120.00017.79
8.1.220.0040.00417.74
8.1.210.0030.00518.77
8.1.200.0080.00317.35
8.1.190.0000.00817.24
8.1.180.0060.00318.10
8.1.170.0050.00318.64
8.1.160.0000.00720.75
8.1.150.0080.00018.88
8.1.140.0000.00819.62
8.1.130.0000.00817.63
8.1.120.0030.00517.50
8.1.110.0070.00017.39
8.1.100.0070.00017.50
8.1.90.0050.00217.46
8.1.80.0000.00717.49
8.1.70.0040.00417.39
8.1.60.0000.00717.64
8.1.50.0040.00417.64
8.1.40.0030.00517.46
8.1.30.0040.00417.71
8.1.20.0040.00417.71
8.1.10.0040.00417.68
8.1.00.0030.00517.57
8.0.300.0020.00518.77
8.0.290.0040.00417.00
8.0.280.0020.00518.50
8.0.270.0070.00017.22
8.0.260.0040.00417.30
8.0.250.0040.00416.96
8.0.240.0060.00317.08
8.0.230.0000.00817.00
8.0.220.0080.00017.04
8.0.210.0040.00416.99
8.0.200.0030.00316.98
8.0.190.0000.00717.07
8.0.180.0030.00617.07
8.0.170.0060.00317.05
8.0.160.0070.00017.02
8.0.150.0040.00416.86
8.0.140.0040.00416.93
8.0.130.0060.00013.41
8.0.120.0030.00517.00
8.0.110.0080.00016.93
8.0.100.0040.00316.90
8.0.90.0000.00716.98
8.0.80.0040.01217.05
8.0.70.0040.00417.04
8.0.60.0040.00416.80
8.0.50.0000.00816.86
8.0.30.0070.01217.35
8.0.20.0070.01317.41
8.0.10.0040.00417.12
8.0.00.0060.01116.92
7.4.330.0030.00316.73
7.4.320.0030.00316.48
7.4.300.0040.00416.63
7.4.290.0030.00316.59
7.4.280.0080.00016.58
7.4.270.0000.00716.61
7.4.260.0000.00716.53
7.4.250.0080.00016.48
7.4.240.0030.00316.59
7.4.230.0000.00716.68
7.4.220.0090.00916.73
7.4.210.0070.00716.69
7.4.200.0000.00716.45
7.4.160.0080.00816.60
7.4.150.0040.01217.40
7.4.140.0160.00617.86
7.4.130.0120.00616.60
7.4.120.0130.00616.60
7.4.110.0100.00716.63
7.4.100.0150.00316.45
7.4.90.0120.00616.50
7.4.80.0060.01619.39
7.4.70.0090.00916.71
7.4.60.0060.01016.49
7.4.50.0030.00816.69
7.4.40.0090.01216.54
7.4.30.0140.00416.68
7.4.00.0070.01014.92
7.3.330.0030.00313.41
7.3.320.0030.00313.39
7.3.310.0000.00716.32
7.3.300.0040.00416.33
7.3.290.0050.01016.42
7.3.280.0060.00916.43
7.3.270.0110.00817.40
7.3.260.0100.00716.65
7.3.250.0100.00816.59
7.3.240.0080.01016.65
7.3.230.0070.01116.46
7.3.210.0100.00716.54
7.3.200.0160.00016.47
7.3.190.0090.00916.70
7.3.180.0100.00716.44
7.3.170.0100.00616.45
7.3.160.0070.01016.56
7.3.120.0090.00915.10
7.3.110.0090.00715.06
7.3.100.0050.01014.81
7.3.90.0070.00814.95
7.3.80.0040.00814.81
7.3.70.0110.00314.98
7.3.60.0020.01014.86
7.3.50.0000.01414.90
7.3.40.0050.00814.96
7.3.30.0030.01014.92
7.3.20.0020.01116.68
7.3.10.0090.00316.79
7.3.00.0110.00316.76
7.2.330.0110.00716.77
7.2.320.0100.01016.76
7.2.310.0100.00616.67
7.2.300.0150.00516.74
7.2.290.0130.00616.78
7.2.250.0090.00915.24
7.2.240.0110.00615.15
7.2.230.0060.01015.00
7.2.220.0080.00515.21
7.2.210.0050.00715.21
7.2.200.0060.00915.02
7.2.190.0060.00914.96
7.2.180.0050.00715.10
7.2.170.0080.00615.02
7.2.60.0130.00316.70
7.2.00.0070.01019.44
7.1.330.0020.01415.82
7.1.320.0040.00915.82
7.1.310.0040.00715.84
7.1.300.0000.01315.86
7.1.290.0030.01315.91
7.1.280.0100.00515.75
7.1.270.0060.00815.71
7.1.260.0030.00615.97
7.1.200.0140.00715.98
7.1.100.0060.00918.41
7.1.70.0050.00517.30
7.1.60.0100.01319.13
7.1.50.0100.01316.98
7.1.00.0070.07322.54
7.0.200.0000.00716.49
7.0.60.0070.07319.85
7.0.50.0030.05017.97
7.0.40.0030.04320.27
7.0.30.0370.07020.07
7.0.20.0330.07720.15
7.0.10.0070.08320.16
7.0.00.0070.04320.05
5.6.280.0070.03321.13
5.6.210.0070.06020.48
5.6.200.0100.08018.29
5.6.190.0130.08020.61
5.6.180.0230.05320.50
5.6.170.0300.07320.57
5.6.160.0100.07720.46
5.6.150.0100.07018.18
5.6.140.0030.08718.24
5.6.130.0070.07318.14
5.6.120.0030.04721.00
5.6.110.0100.07720.97
5.6.100.0070.05021.11
5.6.90.0070.08721.01
5.6.80.0070.07020.49
5.5.350.0300.07320.39
5.5.340.0170.07017.97
5.5.330.0130.08320.37
5.5.320.0370.06720.31
5.5.310.0200.07720.29
5.5.300.0070.07718.05
5.5.290.0070.06018.04
5.5.280.0030.05320.91
5.5.270.0030.04320.81
5.5.260.0100.08020.77
5.5.250.0130.07320.63
5.5.240.0070.08720.38

preferences:
44.03 ms | 400 KiB | 5 Q