3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Menu { const MENU_CACHE_KEY = 'menu_cache'; /** @var MenuItem[] */ protected $rootNodes = array(); protected $childNodes = array(); /** @var ICache */ protected $cache; /** * @param ICache $cache */ public function setCache(ICache $cache) { $this->cache = $cache; } public function hasCache() { return $this->cache instanceof ICache; } /** * @param MenuItem $menuItem */ public function add(MenuItem $menuItem) { if ($menuItem->isParent()) { $this->addRootNode($menuItem); } else { $this->addChildNode($menuItem); } } /** * @param MenuItem $menuItem */ private function addRootNode(MenuItem $menuItem) { array_push($this->rootNodes, $menuItem); } /** * @param MenuItem $menuItem */ private function addChildNode(MenuItem $menuItem) { $this->ensureChildNodeExists($menuItem->parentId); array_push($this->childNodes[$menuItem->parentId], $menuItem); } /** * @param int $parentId */ private function ensureChildNodeExists($parentId) { if (!isset($this->childNodes[$parentId])) { $this->childNodes[$parentId] = array(); } } /** * @return string */ public function render() { if ($this->hasCache()) { $cached = $this->cache->getItem(self::MENU_CACHE_KEY); if (!empty($cached)) { return $cached; } } $html = '<ul>'; foreach ($this->rootNodes as $menuItem) { $html .= '<li>' . $menuItem->render(); if (!empty($this->childNodes[$menuItem->id])) { $html .= '<ul>'; /** @var $childMenuItem MenuItem */ foreach ($this->childNodes[$menuItem->id] as $childMenuItem) { $html .= '<li>' . $childMenuItem->render() . '</li>'; } $html .= '</ul>'; } $html .= '</li>'; } $html .= '</ul>'; if ($this->hasCache()) { $this->cache->setItem(self::MENU_CACHE_KEY, $html); } return $html; } } class MenuItem { public $id; public $parentId; public $label; public $link; /** * @param array|null $params */ public function __construct($params = null) { if (!is_null($params)) { $this->id = (int) $params['id']; $this->label = $params['label']; $this->link = $params['link']; $this->parentId = (int) $params['parent_id']; } } /** * @return bool */ public function isParent() { return $this->parentId === 0; } /** * @return string */ public function render() { return '<a href="' . $this->link . '">' . $this->label . '</a>'; } } interface ICache { /** * @param string $key * * @return string|null */ public function getItem($key); /** * @param string $key * @param string $value */ public function setItem($key, $value); } $dbResults = array( array('id' => 1, 'parent_id' => 0, 'link' => '#', 'label' => 'Home'), array('id' => 2, 'parent_id' => 0, 'link' => '#', 'label' => 'Portfolio'), array('id' => 3, 'parent_id' => 0, 'link' => '#', 'label' => 'Projects'), array('id' => 4, 'parent_id' => 2, 'link' => '#', 'label' => 'Design'), array('id' => 5, 'parent_id' => 2, 'link' => '#', 'label' => 'Dev'), array('id' => 6, 'parent_id' => 2, 'link' => '#', 'label' => 'Foo'), array('id' => 7, 'parent_id' => 3, 'link' => '#', 'label' => 'Bar'), ); $menu = new Menu(); /* $menu->setCache(new RedisCache()); $menu->setCache(new FileCache()); $menu->setCache(new MemcachedCache()); */ foreach ($dbResults as $row) { $menu->add(new MenuItem($row)); } echo $menu->render();

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.0040.01118.68
8.3.50.0110.00921.15
8.3.40.0150.00018.73
8.3.30.0140.00019.17
8.3.20.0040.00420.33
8.3.10.0050.00320.48
8.3.00.0040.00419.50
8.2.180.0140.00716.88
8.2.170.0070.01422.96
8.2.160.0100.00320.43
8.2.150.0070.00324.18
8.2.140.0040.00424.66
8.2.130.0050.00326.16
8.2.120.0040.00422.06
8.2.110.0040.00720.94
8.2.100.0040.00818.11
8.2.90.0080.00019.21
8.2.80.0050.00317.97
8.2.70.0000.00917.50
8.2.60.0040.00417.93
8.2.50.0090.00018.07
8.2.40.0000.00818.24
8.2.30.0070.00418.11
8.2.20.0030.00617.85
8.2.10.0050.00218.23
8.2.00.0030.00517.71
8.1.280.0070.00725.92
8.1.270.0000.00822.22
8.1.260.0000.00726.35
8.1.250.0040.00428.09
8.1.240.0030.00621.42
8.1.230.0080.00319.48
8.1.220.0040.00417.79
8.1.210.0000.00818.77
8.1.200.0060.00317.48
8.1.190.0060.00317.22
8.1.180.0000.00818.10
8.1.170.0030.00618.64
8.1.160.0030.00622.06
8.1.150.0030.00618.93
8.1.140.0070.00017.38
8.1.130.0040.00417.78
8.1.120.0070.00017.48
8.1.110.0040.00417.55
8.1.100.0040.00417.52
8.1.90.0030.00517.44
8.1.80.0000.00717.41
8.1.70.0000.00817.45
8.1.60.0030.00617.66
8.1.50.0060.00317.55
8.1.40.0040.00417.47
8.1.30.0000.00817.73
8.1.20.0040.00417.76
8.1.10.0000.00817.66
8.1.00.0050.00317.52
8.0.300.0060.00318.77
8.0.290.0040.00416.75
8.0.280.0040.00418.48
8.0.270.0070.00017.32
8.0.260.0040.00417.30
8.0.250.0040.00416.98
8.0.240.0000.00816.95
8.0.230.0050.00217.02
8.0.220.0070.00016.92
8.0.210.0030.00317.00
8.0.200.0030.00317.05
8.0.190.0100.00017.10
8.0.180.0040.00416.93
8.0.170.0000.00817.00
8.0.160.0000.00716.95
8.0.150.0030.00417.00
8.0.140.0040.00417.00
8.0.130.0050.00013.39
8.0.120.0050.00317.04
8.0.110.0040.00417.06
8.0.100.0070.00016.98
8.0.90.0080.00017.07
8.0.80.0030.01316.95
8.0.70.0000.00716.89
8.0.60.0070.00017.02
8.0.50.0000.00816.88
8.0.30.0080.01117.01
8.0.20.0160.00517.40
8.0.10.0040.00416.97
8.0.00.0090.01016.83
7.4.330.0000.00515.12
7.4.320.0030.00316.46
7.4.300.0080.00016.65
7.4.290.0000.00816.65
7.4.280.0040.00416.63
7.4.270.0000.00716.64
7.4.260.0000.00716.61
7.4.250.0040.00416.37
7.4.240.0040.00316.56
7.4.230.0060.00316.67
7.4.220.0070.01316.67
7.4.210.0120.00616.59
7.4.200.0000.00816.73
7.4.160.0030.02116.55
7.4.150.0100.01417.40
7.4.140.0100.00617.86
7.4.130.0050.01216.56
7.4.120.0120.00616.53
7.4.110.0120.00616.75
7.4.100.0070.01016.46
7.4.90.0180.00416.39
7.4.80.0120.00619.39
7.4.70.0070.01316.71
7.4.60.0130.00516.42
7.4.50.0030.00516.48
7.4.40.0090.00916.61
7.4.30.0090.00616.58
7.4.00.0000.01715.13
7.3.330.0050.00013.43
7.3.320.0030.00313.35
7.3.310.0070.00016.23
7.3.300.0000.00716.43
7.3.290.0040.01116.33
7.3.280.0090.01016.45
7.3.270.0110.00817.40
7.3.260.0040.01616.43
7.3.250.0100.00916.45
7.3.240.0130.01016.45
7.3.230.0130.00316.66
7.3.210.0130.00416.51
7.3.200.0000.01719.39
7.3.190.0110.00616.38
7.3.180.0100.00716.34
7.3.170.0110.01116.67
7.3.160.0150.00616.43
7.3.10.0120.00616.38
7.3.00.0230.00016.20
7.2.330.0180.00616.93
7.2.320.0030.01516.77
7.2.310.0030.01316.48
7.2.300.0100.00716.93
7.2.290.0140.00816.60
7.2.130.0070.01416.32
7.2.120.0190.00616.69
7.2.110.0180.00516.52
7.2.100.0140.00716.70
7.2.90.0150.00516.82
7.2.80.0210.00816.92
7.2.70.0110.00616.53
7.2.60.0130.01217.00
7.2.50.0110.01416.55
7.2.40.0130.01016.80
7.2.30.0160.00817.02
7.2.20.0080.01516.77
7.2.10.0120.00916.65
7.2.00.0040.01417.88
7.1.250.0200.00315.37
7.1.100.0040.00418.11
7.1.70.0040.00717.25
7.1.60.0130.01319.32
7.1.50.0120.00916.98
7.1.00.0000.08022.28
7.0.200.0030.00716.73
7.0.140.0030.07322.18
7.0.60.0070.04019.99
7.0.50.0030.04017.93
7.0.40.0170.07720.18
7.0.30.0300.06320.23
7.0.20.0300.05320.22
7.0.10.0030.04320.19
7.0.00.0100.04720.23
5.6.280.0030.07321.05
5.6.210.0000.04320.50
5.6.200.0070.08718.21
5.6.190.0130.08320.48
5.6.180.0370.06320.52
5.6.170.0370.06720.69
5.6.160.0170.06720.50
5.6.150.0030.06318.20
5.6.140.0100.07318.16
5.6.130.0070.08018.21
5.6.120.0070.08021.13
5.6.110.0100.07321.04
5.6.100.0130.08021.01
5.6.90.0130.06321.00
5.6.80.0000.04320.43
5.5.350.0200.05320.45
5.5.340.0070.08018.00
5.5.330.0130.07320.40
5.5.320.0230.05020.34
5.5.310.0200.04320.51
5.5.300.0030.05017.94
5.5.290.0100.06317.96
5.5.280.0070.05720.83
5.5.270.0100.03320.66
5.5.260.0200.07020.67
5.5.250.0030.08020.71
5.5.240.0230.06320.19
5.4.450.0530.08719.20
5.4.440.1000.06319.46
5.4.430.0730.05719.16
5.4.420.0630.00019.51
5.4.410.0700.00019.27
5.4.400.0630.00019.25
5.4.390.0630.00019.09
5.4.380.0670.00019.12
5.4.370.0630.00019.05
5.4.360.0800.00019.03
5.4.350.0670.00019.21
5.4.340.0630.00019.11
5.4.320.0060.03812.54
5.4.310.0050.03912.53
5.4.300.0010.04212.53
5.4.290.0060.04112.52
5.4.280.0100.04712.42
5.4.270.0070.04512.43
5.4.260.0070.04012.43
5.4.250.0060.03912.42
5.4.240.0060.03912.43
5.4.230.0040.04012.42
5.4.220.0060.03712.42
5.4.210.0080.04212.42
5.4.200.0060.03812.41
5.4.190.0060.03612.41
5.4.180.0020.04112.42
5.4.170.0070.03512.42
5.4.160.0070.03512.42
5.4.150.0070.03512.42
5.4.140.0060.03812.10
5.4.130.0060.03712.09
5.4.120.0040.03912.05
5.4.110.0050.03812.04
5.4.100.0030.04012.04
5.4.90.0080.03612.04
5.4.80.0110.03512.04
5.4.70.0040.03712.04
5.4.60.0040.03712.04
5.4.50.0080.03812.04
5.4.40.0060.03412.03
5.4.30.0040.03812.02
5.4.20.0060.03512.02
5.4.10.0070.03312.02
5.4.00.0060.03911.52
5.3.290.0050.04012.80
5.3.280.0080.04512.72
5.3.270.0060.03912.73
5.3.260.0060.03812.73
5.3.250.0050.03812.74
5.3.240.0080.03512.73
5.3.230.0040.03912.73
5.3.220.0060.03712.70
5.3.210.0060.04012.69
5.3.200.0050.03912.69
5.3.190.0070.04212.69
5.3.180.0080.03412.69
5.3.170.0080.03612.69
5.3.160.0070.03612.69
5.3.150.0060.03712.70
5.3.140.0080.03412.68
5.3.130.0070.03812.68
5.3.120.0060.03912.68
5.3.110.0070.03812.67
5.3.100.0020.04012.16
5.3.90.0090.03312.14
5.3.80.0040.03812.14
5.3.70.0060.03712.13
5.3.60.0050.03812.12
5.3.50.0050.03712.07
5.3.40.0060.03612.07
5.3.30.0060.03512.03
5.3.20.0060.03811.81
5.3.10.0090.03511.77
5.3.00.0050.04311.76
5.2.170.0050.0309.27
5.2.160.0070.0279.27
5.2.150.0060.0309.27
5.2.140.0030.0329.26
5.2.130.0080.0279.22
5.2.120.0060.0289.23
5.2.110.0040.0319.23
5.2.100.0040.0309.23
5.2.90.0050.0289.23
5.2.80.0050.0309.22
5.2.70.0060.0309.22
5.2.60.0070.0319.17
5.2.50.0070.0329.14
5.2.40.0030.0339.12
5.2.30.0050.0319.10
5.2.20.0060.0339.09
5.2.10.0060.0279.00
5.2.00.0050.0298.86
5.1.60.0040.0278.14
5.1.50.0070.0228.14
5.1.40.0050.0248.13
5.1.30.0040.0278.47
5.1.20.0020.0298.49
5.1.10.0030.0308.21
5.1.00.0040.0278.22
5.0.50.0000.0246.70
5.0.40.0040.0186.56
5.0.30.0040.0376.38
5.0.20.0040.0186.34
5.0.10.0040.0216.32
5.0.00.0040.0306.31
4.4.90.0030.0154.78
4.4.80.0020.0164.75
4.4.70.0020.0164.75
4.4.60.0040.0144.75
4.4.50.0030.0164.77
4.4.40.0020.0264.71
4.4.30.0030.0154.76
4.4.20.0040.0154.84
4.4.10.0010.0174.85
4.4.00.0030.0254.76
4.3.110.0020.0164.67
4.3.100.0050.0134.66
4.3.90.0040.0134.64
4.3.80.0030.0274.59
4.3.70.0030.0144.63
4.3.60.0040.0204.63
4.3.50.0030.0144.63
4.3.40.0010.0274.54
4.3.30.0030.0153.30
4.3.20.0010.0173.28
4.3.10.0010.0173.24
4.3.00.0000.01316.59

preferences:
37.7 ms | 401 KiB | 5 Q