3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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'), ); 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); } $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.31
8.3.50.0100.01021.04
8.3.40.0090.00618.84
8.3.30.0110.00319.07
8.3.20.0040.00420.16
8.3.10.0080.00020.48
8.3.00.0090.00019.51
8.2.180.0160.00618.41
8.2.170.0070.00722.96
8.2.160.0070.00720.47
8.2.150.0040.00724.18
8.2.140.0080.00024.66
8.2.130.0070.00026.16
8.2.120.0040.00422.10
8.2.110.0000.00920.52
8.2.100.0130.00017.78
8.2.90.0000.00819.36
8.2.80.0000.00817.97
8.2.70.0030.00617.63
8.2.60.0000.00818.04
8.2.50.0070.00318.07
8.2.40.0040.00417.92
8.2.30.0050.00519.80
8.2.20.0030.00517.69
8.2.10.0070.00018.15
8.2.00.0030.00517.68
8.1.280.0110.01125.92
8.1.270.0050.00322.14
8.1.260.0040.00426.35
8.1.250.0060.00628.09
8.1.240.0090.00323.81
8.1.230.0110.00021.03
8.1.220.0040.00417.80
8.1.210.0000.00818.77
8.1.200.0040.00417.48
8.1.190.0080.00017.22
8.1.180.0060.00318.10
8.1.170.0000.00818.56
8.1.160.0050.00221.97
8.1.150.0000.00718.85
8.1.140.0030.00617.46
8.1.130.0070.00017.85
8.1.120.0070.00017.36
8.1.110.0050.00317.52
8.1.100.0000.00717.52
8.1.90.0040.00417.39
8.1.80.0070.00017.47
8.1.70.0070.00017.49
8.1.60.0050.00317.69
8.1.50.0030.00617.61
8.1.40.0000.00817.53
8.1.30.0040.00417.54
8.1.20.0000.00817.68
8.1.10.0030.00517.59
8.1.00.0030.00617.45
8.0.300.0040.00418.77
8.0.290.0040.00417.00
8.0.280.0080.00018.40
8.0.270.0000.00816.93
8.0.260.0030.00317.30
8.0.250.0040.00416.96
8.0.240.0040.00417.03
8.0.230.0030.00317.13
8.0.220.0050.00316.95
8.0.210.0030.00317.02
8.0.200.0000.00817.08
8.0.190.0120.00616.93
8.0.180.0000.00717.03
8.0.170.0000.00917.05
8.0.160.0040.00417.01
8.0.150.0000.00816.99
8.0.140.0040.00416.98
8.0.130.0060.00013.47
8.0.120.0040.00417.05
8.0.110.0040.00416.82
8.0.100.0040.00416.95
8.0.90.0000.00716.82
8.0.80.0060.00916.89
8.0.70.0040.00416.90
8.0.60.0050.00216.88
8.0.50.0040.00416.89
8.0.30.0140.01217.08
8.0.20.0150.00717.44
8.0.10.0040.00417.15
8.0.00.0040.01616.75
7.4.330.0050.00015.15
7.4.320.0000.00616.66
7.4.300.0000.00616.50
7.4.290.0000.00716.57
7.4.280.0040.00416.53
7.4.270.0040.00416.64
7.4.260.0040.00416.64
7.4.250.0000.00816.50
7.4.240.0030.00316.65
7.4.230.0030.00516.39
7.4.220.0060.01316.62
7.4.210.0000.01416.71
7.4.200.0040.00416.59
7.4.160.0110.00716.37
7.4.150.0090.00817.40
7.4.140.0070.01117.86
7.4.130.0100.00716.56
7.4.120.0110.00616.60
7.4.110.0030.01316.71
7.4.100.0040.01516.43
7.4.90.0230.00016.67
7.4.80.0160.00319.39
7.4.70.0030.01516.53
7.4.60.0070.01016.61
7.4.50.0000.00816.66
7.4.40.0110.00616.44
7.4.30.0090.00616.41
7.4.10.0090.01214.84
7.4.00.0060.00915.12
7.3.330.0030.00213.28
7.3.320.0000.00613.23
7.3.310.0000.00716.40
7.3.300.0000.00716.29
7.3.290.0070.00716.43
7.3.280.0050.01216.38
7.3.270.0090.00917.40
7.3.260.0120.01216.28
7.3.250.0120.00416.56
7.3.240.0090.00616.43
7.3.230.0090.00616.39
7.3.210.0070.01116.61
7.3.200.0100.00619.39
7.3.190.0070.01016.61
7.3.180.0080.00816.33
7.3.170.0100.00616.59
7.3.160.0070.01516.44
7.3.130.0070.00714.86
7.3.120.0090.00915.16
7.3.110.0060.01514.99
7.3.100.0060.00914.76
7.3.90.0070.00715.11
7.3.80.0030.00615.03
7.3.70.0030.01414.99
7.3.60.0090.00014.70
7.3.50.0080.00414.68
7.3.40.0040.00814.67
7.3.30.0040.01114.86
7.3.20.0100.01016.71
7.3.10.0090.00616.50
7.3.00.0100.00616.52
7.2.330.0040.01316.86
7.2.320.0100.01016.93
7.2.310.0140.00316.91
7.2.300.0110.00616.75
7.2.290.0140.01016.86
7.2.260.0000.01715.27
7.2.250.0000.01514.86
7.2.240.0000.01615.03
7.2.230.0090.00614.97
7.2.220.0100.00315.11
7.2.210.0060.00915.25
7.2.200.0040.01115.18
7.2.190.0070.00315.01
7.2.180.0030.00615.23
7.2.170.0070.00714.87
7.2.160.0000.01114.98
7.2.150.0070.00716.66
7.2.140.0100.00316.84
7.2.130.0090.00816.63
7.2.120.0180.00916.88
7.2.110.0070.01216.72
7.2.100.0080.00616.53
7.2.90.0120.00816.71
7.2.80.0090.00916.66
7.2.70.0090.00816.81
7.2.60.0090.00816.60
7.2.50.0090.01116.68
7.2.40.0080.01516.79
7.2.30.0070.01116.54
7.2.20.0100.00716.72
7.2.10.0150.00816.80
7.2.00.0120.00517.75
7.1.330.0070.00316.05
7.1.320.0060.01215.62
7.1.310.0040.00815.91
7.1.300.0130.00315.82
7.1.290.0070.00715.87
7.1.280.0040.01116.00
7.1.270.0040.01115.85
7.1.260.0060.00315.90
7.1.250.0070.00715.52
7.1.240.0040.01115.82
7.1.230.0040.00815.91
7.1.220.0060.00615.76
7.1.210.0130.00315.96
7.1.200.0090.00915.88
7.1.190.0080.00815.77
7.1.180.0100.00616.02
7.1.170.0100.00715.73
7.1.160.0090.00315.81
7.1.150.0030.00615.64
7.1.140.0120.00315.71
7.1.130.0030.00615.54
7.1.120.0000.01215.84
7.1.110.0070.01115.89
7.1.100.0080.00416.76
7.1.90.0070.01015.74
7.1.80.0040.00415.89
7.1.70.0040.00716.59
7.1.60.0100.01017.60
7.1.50.0070.00916.47
7.1.40.0080.00415.89
7.1.30.0090.00615.93
7.1.20.0030.00615.74
7.1.10.0070.00415.69
7.1.00.0050.04119.04
7.0.330.0060.00615.44
7.0.320.0120.00315.37
7.0.310.0090.00615.32
7.0.300.0110.00615.44
7.0.290.0000.00915.43
7.0.280.0090.00915.27
7.0.270.0060.00615.61
7.0.260.0040.01115.41
7.0.250.0100.00315.50
7.0.240.0110.00415.60
7.0.230.0030.01215.27
7.0.220.0040.00815.50
7.0.210.0030.01015.25
7.0.200.0030.00515.96
7.0.190.0070.00315.47
7.0.180.0060.00315.56
7.0.170.0030.01015.16
7.0.160.0070.00715.47
7.0.150.0120.00315.55
7.0.140.0090.03718.84
7.0.130.0060.00915.56
7.0.120.0110.00015.57
7.0.110.0070.00715.38
7.0.100.0060.00915.39
7.0.90.0130.00615.53
7.0.80.0080.00415.52
7.0.70.2150.02717.77
7.0.60.0080.02217.77
7.0.50.0020.02716.60
7.0.40.0040.02616.94
7.0.30.0260.02516.92
7.0.20.0100.02516.64
7.0.10.0270.03416.78
7.0.00.0060.02416.87
5.6.400.0040.00814.33
5.6.390.0000.01114.55
5.6.380.0070.00714.37
5.6.370.0040.00714.64
5.6.360.0100.00614.77
5.6.350.0040.00814.52
5.6.340.0090.00914.66
5.6.330.0110.00314.61
5.6.320.0070.01014.81
5.6.310.0070.01014.83
5.6.300.0100.00714.82
5.6.290.0030.01414.86
5.6.280.0070.04817.72
5.6.270.0060.00814.36
5.6.260.0060.00914.79
5.6.250.0000.01014.64
5.6.240.0060.00914.61
5.6.230.0070.00714.54
5.6.220.0030.01014.27
5.6.210.0050.02617.54
5.6.200.0070.04816.61
5.6.190.0050.03017.48
5.6.180.0050.04717.52
5.6.170.0150.02317.47
5.6.160.0070.04717.60
5.6.150.0060.03616.47
5.6.140.0020.03716.48
5.6.130.0050.04716.39
5.6.120.0050.04817.77
5.6.110.0080.04317.73
5.6.100.0060.03217.75
5.6.90.0070.04717.71
5.6.80.0050.04317.38
5.6.70.0060.04317.37
5.6.60.0100.00314.26
5.6.50.0070.00714.35
5.6.40.0070.01114.33
5.6.30.0040.00814.53
5.6.20.0030.01014.36
5.6.10.0080.00814.34
5.6.00.0070.00314.50
5.5.380.0030.00614.22
5.5.370.0030.01214.35
5.5.360.0040.01114.61
5.5.350.0050.04017.50
5.5.340.0050.02516.30
5.5.330.0080.02217.41
5.5.320.0080.04517.40
5.5.310.0240.01817.45
5.5.300.0030.04816.21
5.5.290.0070.03016.27
5.5.280.0030.04717.66
5.5.270.0060.04217.54
5.5.260.0050.03017.73
5.5.250.0080.03717.47
5.5.240.0070.02317.47
5.5.230.0070.01014.62
5.5.220.0080.00614.48
5.5.210.0070.00714.43
5.5.200.0090.00614.59
5.5.190.0030.00614.61
5.5.180.0000.01414.48
5.5.170.0000.01414.31
5.5.160.0060.00614.35
5.5.150.0060.00314.53
5.5.140.0090.00614.39
5.5.130.0080.00414.36
5.5.120.0080.00814.71
5.5.110.0120.00314.33
5.5.100.0030.00714.03
5.5.90.0040.00714.45
5.5.80.0030.00914.47
5.5.70.0040.01114.59
5.5.60.0030.01014.60
5.5.50.0080.00614.22
5.5.40.0000.01114.39
5.5.30.0100.00014.01
5.5.20.0100.00314.34
5.5.10.0000.01813.98
5.5.00.0000.01514.31
5.4.450.0540.02415.56
5.4.440.0430.03015.24
5.4.430.0580.02815.28
5.4.420.0350.00315.41
5.4.410.0330.00515.36
5.4.400.0390.00115.25
5.4.390.0300.00515.22
5.4.380.0330.00415.23
5.4.370.0350.00215.21
5.4.360.0320.00315.05
5.4.350.0320.00415.19
5.4.340.0350.00215.27
5.4.330.0040.00711.00
5.4.320.0060.01911.97
5.4.310.0060.02112.00
5.4.300.0060.02211.84
5.4.290.0020.02311.86
5.4.280.0050.02111.68
5.4.270.0060.02111.79
5.4.260.0040.02311.69
5.4.250.0040.02111.85
5.4.240.0060.02211.77
5.4.230.0030.02111.74
5.4.220.0050.02111.88
5.4.210.0070.02111.74
5.4.200.0060.02011.73
5.4.190.0030.02211.74
5.4.180.0030.02411.78
5.4.170.0060.02311.68
5.4.160.0040.02411.85
5.4.150.0060.02211.89
5.4.140.0050.02211.79
5.4.130.0050.02111.55
5.4.120.0050.01911.51
5.4.110.0030.02211.48
5.4.100.0060.02111.57
5.4.90.0080.01811.70
5.4.80.0050.02111.65
5.4.70.0110.01511.68
5.4.60.0030.02311.53
5.4.50.0040.02311.72
5.4.40.0060.02111.62
5.4.30.0040.02111.73
5.4.20.0040.02111.73
5.4.10.0070.01711.54
5.4.00.0040.02011.43
5.3.290.0070.03912.80
5.3.280.0040.04012.72
5.3.270.0110.03512.73
5.3.260.0060.04012.73
5.3.250.0110.03212.74
5.3.240.0050.03812.73
5.3.230.0060.03812.73
5.3.220.0080.03512.71
5.3.210.0070.03812.70
5.3.200.0060.03812.70
5.3.190.0030.04412.70
5.3.180.0050.03812.69
5.3.170.0080.03512.70
5.3.160.0050.03812.69
5.3.150.0050.03812.70
5.3.140.0060.03812.68
5.3.130.0080.03812.68
5.3.120.0050.04112.68
5.3.110.0070.03812.68
5.3.100.0040.04112.17
5.3.90.0050.03812.15
5.3.80.0090.03412.14
5.3.70.0070.03712.14
5.3.60.0050.03712.12
5.3.50.0100.03312.07
5.3.40.0060.03712.07
5.3.30.0030.03812.03
5.3.20.0050.03711.81
5.3.10.0060.03511.77
5.3.00.0050.04111.76
5.2.170.0050.0349.27
5.2.160.0050.0309.27
5.2.150.0030.0329.27
5.2.140.0070.0289.26
5.2.130.0050.0299.23
5.2.120.0040.0299.22
5.2.110.0070.0279.23
5.2.100.0030.0319.23
5.2.90.0050.0299.23
5.2.80.0080.0289.22
5.2.70.0040.0339.22
5.2.60.0060.0309.18
5.2.50.0030.0339.14
5.2.40.0040.0309.12
5.2.30.0050.0309.10
5.2.20.0070.0289.09
5.2.10.0030.0308.99
5.2.00.0060.0288.86
5.1.60.0040.0258.14
5.1.50.0060.0248.14
5.1.40.0040.0288.13
5.1.30.0060.0258.47
5.1.20.0060.0268.49
5.1.10.0040.0278.21
5.1.00.0020.0288.21
5.0.50.0020.0226.70
5.0.40.0030.0196.56
5.0.30.0040.0326.38
5.0.20.0020.0216.34
5.0.10.0050.0196.32
5.0.00.0030.0316.31
4.4.90.0040.0144.78
4.4.80.0010.0174.75
4.4.70.0030.0154.75
4.4.60.0030.0154.75
4.4.50.0000.0184.77
4.4.40.0020.0264.71
4.4.30.0010.0174.76
4.4.20.0060.0124.84
4.4.10.0020.0164.85
4.4.00.0030.0254.76
4.3.110.0010.0174.66
4.3.100.0010.0184.66
4.3.90.0010.0174.64
4.3.80.0010.0264.59
4.3.70.0030.0144.63
4.3.60.0030.0154.63
4.3.50.0030.0154.63
4.3.40.0030.0234.54
4.3.30.0010.0163.31
4.3.20.0030.0153.29
4.3.10.0010.0173.23
4.3.00.0300.02716.59

preferences:
36.94 ms | 400 KiB | 5 Q