3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Graph { private $graph = []; private $nodes = []; public function __construct(array $edges) { foreach ($edges as $edge => $weight) { $start = $edge[0]; $end = $edge[1]; $this->graph[$start][$end] = $weight; $this->graph[$end][$start] = $weight; $this->nodes[$start] = [ 'L' => INF, 'Q' => null, 'final' => false, ]; $this->nodes[$end] = [ 'L' => INF, 'Q' => null, 'final' => false, ]; } } public function find($start, $end, $initStartNode = true) { if ($initStartNode) { $this->initStartNode($start); } $nodes = $this->getNextNodes($start); $startLength = $this->getLength($start); foreach($nodes as $node => $weight) { $currentLength = $this->getLength($node); if ($startLength + $weight < $currentLength) { $this->setLength($node, $startLength + $weight); $this->setFromNode($node, $start); } } $nextNode = $this->getNextMinimumNode(); if (is_null($nextNode)) { return $this->getNodes(); } $this->setFinal($nextNode); $this->find($nextNode, $end, false); // var_dump($this->getGraph()); var_dump($this->getNodes()); // die(); } protected function getNextMinimumNode() { $nodes = $this->getNodes(); $min = INF; $minNode = null; foreach ($nodes as $node => $data) { if ($data['final']) { continue; } $nodeLength = $this->getLength($node); if ($nodeLength < $min) { $min = $nodeLength; $minNode = $node; } } return $minNode; } protected function initStartNode($node) { $this->setLength($node, 0); $this->setFromNode($node, '-'); $this->setFinal($node); } protected function getNextNodes($node) { return $this->graph[$node]; } protected function setLength($node, $value) { $this->nodes[$node]['L'] = $value; } protected function getLength($node) { return $this->nodes[$node]['L']; } protected function setFromNode($node, $value) { $this->nodes[$node]['Q'] = $value; } protected function setFinal($node) { $this->nodes[$node]['final'] = true; } public function getGraph() { return $this->graph; } public function getNodes() { return $this->nodes; } } $g = new Graph([ 'AB' => 5, 'AC' => 2, 'CB' => 2, 'CD' => 8, 'BE' => 7, 'DE' => 1, ]); $result = $g->find('A', 'E'); var_dump($result); // var_dump($g->getGraph()); // var_dump($g->getNodes());

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.40.0100.01318.79
8.3.30.0120.00418.71
8.3.20.0070.00322.09
8.3.10.0070.01320.54
8.3.00.0060.00323.66
8.2.170.0130.01022.96
8.2.160.0200.00320.39
8.2.150.0070.00324.18
8.2.140.0030.00624.66
8.2.130.0030.00622.11
8.2.120.0100.01026.35
8.2.110.0060.00320.96
8.2.100.0030.01017.91
8.2.90.0030.00519.34
8.2.80.0030.00617.97
8.2.70.0060.00317.63
8.2.60.0000.01417.80
8.2.50.0030.00519.14
8.2.40.0100.00320.39
8.2.30.0000.00820.53
8.2.20.0050.00318.20
8.2.10.0030.00518.16
8.2.00.0040.00418.22
8.1.270.0030.00620.74
8.1.260.0060.00326.35
8.1.250.0060.00328.09
8.1.240.0110.00419.13
8.1.230.0060.00619.16
8.1.220.0030.00617.74
8.1.210.0000.00818.77
8.1.200.0000.01017.35
8.1.190.0040.00417.23
8.1.180.0030.00518.10
8.1.170.0040.00418.39
8.1.160.0080.00018.91
8.1.150.0000.00820.20
8.1.140.0000.00819.59
8.1.130.0020.00519.05
8.1.120.0040.00417.52
8.1.110.0050.00317.52
8.1.100.0040.00417.52
8.1.90.0000.00817.43
8.1.80.0030.00617.41
8.1.70.0000.00717.37
8.1.60.0030.00617.50
8.1.50.0000.00917.51
8.1.40.0000.00917.43
8.1.30.0030.00617.64
8.1.20.0000.00817.67
8.1.10.0040.00417.51
8.1.00.0050.00317.57
8.0.300.0060.00318.77
8.0.290.0030.00516.63
8.0.280.0000.00718.41
8.0.270.0020.00517.23
8.0.260.0000.00717.16
8.0.250.0080.00016.96
8.0.240.0000.00916.98
8.0.230.0000.00716.86
8.0.220.0040.00416.81
8.0.210.0040.00416.84
8.0.200.0000.00717.02
8.0.190.0080.00017.04
8.0.180.0000.00816.94
8.0.170.0090.00016.84
8.0.160.0100.00016.76
8.0.150.0040.00416.80
8.0.140.0040.00416.80
8.0.130.0000.00713.30
8.0.120.0040.00416.92
8.0.110.0000.00816.94
8.0.100.0080.00016.89
8.0.90.0040.00416.97
8.0.80.0070.01016.91
8.0.70.0000.00816.96
8.0.60.0000.00916.99
8.0.50.0060.00316.90
8.0.30.0120.00816.95
8.0.20.0100.01617.40
8.0.10.0040.00416.90
8.0.00.0140.00816.89
7.4.330.0030.00315.55
7.4.320.0050.00216.55
7.4.300.0080.00016.45
7.4.290.0040.00716.45
7.4.280.0000.00716.54
7.4.270.0040.00416.64
7.4.260.0050.00316.59
7.4.250.0000.00816.54
7.4.240.0040.00416.65
7.4.230.0080.00016.74
7.4.220.0070.01916.65
7.4.210.0130.00516.73
7.4.200.0000.00816.51
7.4.160.0130.00616.53
7.4.150.0110.01117.40
7.4.140.0120.01117.86
7.4.130.0090.01216.58
7.4.120.0120.01116.53
7.4.110.0090.00916.38
7.4.100.0170.00316.46
7.4.90.0090.01616.68
7.4.80.0070.01119.39
7.4.70.0070.01816.65
7.4.60.0060.01216.68
7.4.50.0090.00616.34
7.4.40.0120.00616.58
7.4.30.0140.01016.52
7.4.10.0070.01315.04
7.4.00.0060.01214.85
7.3.330.0030.00313.20
7.3.320.0030.00313.34
7.3.310.0040.00416.21
7.3.300.0070.00016.36
7.3.290.0070.00816.37
7.3.280.0090.00916.37
7.3.270.0100.01017.40
7.3.260.0130.00916.45
7.3.250.0090.01316.45
7.3.240.0150.01016.46
7.3.230.0110.01116.39
7.3.210.0120.00616.35
7.3.200.0090.00916.35
7.3.190.0100.00916.29
7.3.180.0090.00916.30
7.3.170.0150.00416.29
7.3.160.0060.01216.70
7.3.130.0060.01314.73
7.3.120.0050.01315.09
7.3.110.0070.00914.85
7.3.100.0120.00214.65
7.3.90.0080.00714.85
7.3.80.0030.01014.93
7.3.70.0030.01114.79
7.3.60.0100.00314.96
7.3.50.0090.00714.85
7.3.40.0060.00814.92
7.3.30.0020.00914.74
7.3.20.0050.00916.60
7.3.10.0080.00816.60
7.3.00.0060.00716.53
7.2.330.0110.01116.78
7.2.320.0140.00416.80
7.2.310.0130.01316.73
7.2.300.0090.01216.94
7.2.290.0090.00916.47
7.2.260.0100.01014.95
7.2.250.0050.01415.24
7.2.240.0070.01215.05
7.2.230.0040.01315.03
7.2.220.0070.00415.13
7.2.210.0050.01315.29
7.2.200.0050.00815.07
7.2.190.0050.00915.11
7.2.180.0090.00415.12
7.2.170.0080.00615.08
7.2.160.0110.00014.93
7.2.150.0030.01016.84
7.2.140.0040.01516.77
7.2.130.0040.00817.12
7.2.120.0030.01016.99
7.2.110.0050.01016.69
7.2.100.0090.00616.98
7.2.90.0030.01316.94
7.2.80.0130.00316.74
7.2.70.0110.00417.07
7.2.60.0060.00717.08
7.2.50.0030.00616.76
7.2.40.0120.00617.00
7.2.30.0080.01216.77
7.2.20.0060.01017.12
7.2.10.0110.00417.07
7.2.00.0060.00616.70
7.1.330.0050.01115.94
7.1.320.0070.00615.82
7.1.310.0040.00915.76
7.1.300.0040.01215.93
7.1.290.0070.00715.74
7.1.280.0070.00515.59
7.1.270.0060.00615.68
7.1.260.0030.01015.84
7.1.250.0080.00515.77
7.1.240.0030.01015.91
7.1.230.0030.01316.02
7.1.220.0030.00615.84
7.1.210.0090.00315.93
7.1.200.0060.00615.78
7.1.190.0030.00815.81
7.1.180.0080.00315.51
7.1.170.0030.00615.85
7.1.160.0040.01215.82
7.1.150.0040.00515.84
7.1.140.0030.01015.82
7.1.130.0090.00315.46
7.1.120.0040.01115.69
7.1.110.0100.00315.73
7.1.100.0090.00015.85
7.1.90.0060.00315.77
7.1.80.0070.00715.82
7.1.70.0050.00816.46
7.1.60.0030.01016.67
7.1.50.0090.00615.78
7.1.40.0070.00315.64
7.1.30.0070.00715.63
7.1.20.0180.04527.13
7.1.10.0080.04419.05
7.1.00.0100.03219.06
7.0.330.0000.01015.52
7.0.320.0000.01215.30
7.0.310.0030.00615.44
7.0.300.0050.00515.53
7.0.290.0060.01015.45
7.0.280.0030.00615.44
7.0.270.0070.00315.40
7.0.260.0030.01015.40
7.0.250.0070.00415.48
7.0.240.0110.00415.35
7.0.230.0000.00815.36
7.0.220.0070.01015.43
7.0.210.0030.01015.51
7.0.200.0080.00916.04
7.0.190.0070.00715.58
7.0.180.0030.00915.18
7.0.170.0040.00815.55
7.0.160.0080.03618.72
7.0.150.0080.04718.73
7.0.140.0110.03018.71
7.0.130.0080.03718.69
7.0.120.0070.04518.64
7.0.110.0080.05018.49
7.0.100.0030.04218.60
7.0.90.0100.03018.44
7.0.80.0130.03018.53
7.0.70.0080.03718.68
7.0.60.0070.04018.69
7.0.50.0030.04318.55
7.0.40.0120.03017.72
7.0.30.0090.02917.59
7.0.20.0070.03217.52
7.0.10.0090.03117.66
7.0.00.0100.03017.70
5.6.400.0070.00714.57
5.6.390.0070.00714.57
5.6.380.0000.00914.01
5.6.370.0000.01114.15
5.6.360.0040.01214.74
5.6.350.0190.00014.89
5.6.340.0030.01014.36
5.6.330.0040.00714.73
5.6.320.0030.01314.31
5.6.310.0040.01114.52
5.6.300.0030.01314.18
5.6.290.0070.00714.69
5.6.280.0070.00714.79
5.6.270.0040.01114.79
5.6.260.0040.00814.59
5.6.250.0000.01614.36
5.6.240.0000.01414.68
5.6.230.0040.01114.57
5.6.220.0140.00014.68
5.6.210.0120.00314.43
5.6.200.0150.00014.66
5.6.190.0030.01014.45
5.6.180.0040.00914.64
5.6.170.0130.00314.64
5.6.160.0000.01614.53
5.6.150.0060.00914.20
5.6.140.0060.00914.76
5.6.130.0110.00314.53
5.6.120.0030.00914.29
5.6.110.0060.00614.89
5.6.100.0070.01014.53
5.6.90.0030.00914.79
5.6.80.0040.00714.28
5.6.70.0040.01114.65
5.6.60.0060.00314.49
5.6.50.0040.00714.40
5.6.40.0000.01614.41
5.6.30.0040.00814.29
5.6.20.0110.00414.34
5.6.10.0030.00614.43
5.6.00.0040.00414.29

preferences:
58.85 ms | 400 KiB | 5 Q