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) || !$this->categoryExists($this->roots, $parent)) { throw new InvalidArgumentException(); } if ($parent === null) { $this->tree[$category] = [] } $node = $findNode($parent, $this->root); if(!isset($node)) { throw new InvalidArgumentException(); } $node[$category] = []; } public function getChildren($parent) { $node = $findNode($parent, $this->root); 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($node as $node) { if (in_array($category, $this->getChildren($node))) { return true; } $exists = $this->categoryExists($node, $category); } return $exists; } public function add(&$node, $category, $parent) { if (in_array($parent, $node)) { return } } } // 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)
5.6.160.0100.07720.52
5.6.150.0000.08718.18
5.6.140.0030.04018.15
5.6.130.0130.03318.24
5.6.120.0130.04020.97
5.6.110.0200.06021.12
5.6.100.0200.07320.96
5.6.90.0230.07321.04
5.6.80.0070.07320.44
5.5.300.0100.07717.95
5.5.290.0030.04718.07
5.5.280.0000.04320.77
5.5.270.0000.04320.80
5.5.260.0000.05320.80
5.5.250.0130.04720.77
5.5.240.0130.06320.35

preferences:
144.85 ms | 1394 KiB | 7 Q