3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* 构造如下一棵树: a / | \ b c d / \ | e f g | h 深度优先遍历(DFT): a b e f h c d g 广度优先遍历(BFT): a b c d e f g h */ $h = new TreeNode('h'); $g = new TreeNode('g'); $f = new TreeNode('f', [$h]); $e = new TreeNode('e'); $d = new TreeNode('d', [$g]); $c = new TreeNode('c'); $b = new TreeNode('b', [$e, $f]); $a = new TreeNode('a', [$b, $c, $d]); $tree = &$a; dft($tree); echo "\n----\n"; bft($tree); exit(); class TreeNode { public $data; public $children = []; public function __construct($data, $children = []) { $this->data = $data; $this->children = array_merge($this->children, $children); } } /** * depth first traversal * * 借助栈实现 */ function dft($tree) { $stack = array($tree, "\n"); while (!empty($stack)) { $node = array_pop($stack); if (is_string($node)) { echo $node; } else { echo $node->data; if ($node->children) { array_push($stack, "\n"); $children = array_reverse($node->children); foreach ($children as $c) { array_push($stack, "\t"); array_push($stack, $c); } array_push($stack, "\n"); } } } } /** * breadth first traversal * * 借助队列实现 */ function bft($tree) { $queue = array($tree, "\n"); while(!empty($queue)) { $node = array_shift($queue); if (is_string($node)) { echo $node; } else { echo $node->data; if ($node->children) { foreach ($node->children as $c) { array_push($queue, $c); array_push($queue, "\t"); } array_push($queue, "\n"); } } } }

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.0060.00916.75
8.3.50.0130.00718.18
8.3.40.0070.00718.97
8.3.30.0040.01118.79
8.3.20.0080.00020.34
8.3.10.0080.00021.73
8.3.00.0050.00318.04
8.2.180.0130.00318.29
8.2.170.0160.00622.96
8.2.160.0080.00521.04
8.2.150.0050.00524.18
8.2.140.0000.00824.66
8.2.130.0040.00422.16
8.2.120.0080.00026.35
8.2.110.0030.00721.07
8.2.100.0090.00317.97
8.2.90.0050.00318.03
8.2.80.0000.00918.94
8.2.70.0000.00917.63
8.2.60.0040.00417.80
8.2.50.0030.00618.07
8.2.40.0040.00418.16
8.2.30.0080.00018.02
8.2.20.0070.00018.00
8.2.10.0000.00718.23
8.2.00.0040.00418.39
8.1.280.0070.01125.92
8.1.270.0030.00520.75
8.1.260.0040.00426.35
8.1.250.0060.00328.09
8.1.240.0100.00022.00
8.1.230.0040.00820.80
8.1.220.0040.00417.74
8.1.210.0000.00818.78
8.1.200.0100.00017.35
8.1.190.0000.00917.35
8.1.180.0040.00418.10
8.1.170.0000.00818.71
8.1.160.0070.00018.96
8.1.150.0050.00320.12
8.1.140.0040.00419.57
8.1.130.0000.00719.02
8.1.120.0000.00917.41
8.1.110.0040.00417.40
8.1.100.0090.00017.45
8.1.90.0040.00417.55
8.1.80.0030.00517.46
8.1.70.0050.00217.54
8.1.60.0040.00417.52
8.1.50.0030.00517.45
8.1.40.0040.00417.45
8.1.30.0000.00817.68
8.1.20.0050.00317.62
8.1.10.0050.00317.65
8.1.00.0000.00817.44
8.0.300.0040.00420.16
8.0.290.0050.00316.88
8.0.280.0050.00218.42
8.0.270.0050.00316.93
8.0.260.0030.00320.72
8.0.250.0030.00317.03
8.0.240.0030.00517.05
8.0.230.0020.00517.08
8.0.220.0030.00316.91
8.0.210.0030.00316.84
8.0.200.0070.00017.13
8.0.190.0070.00016.96
8.0.180.0040.00416.91
8.0.170.0050.00317.00
8.0.160.0000.00917.09
8.0.150.0040.00416.98
8.0.140.0020.00516.84
8.0.130.0030.00313.42
8.0.120.0040.00416.96
8.0.110.0080.00017.04
8.0.100.0040.00417.00
8.0.90.0040.00416.89
8.0.80.0130.00916.95
8.0.70.0040.00417.05
8.0.60.0040.00416.97
8.0.50.0040.00416.81
8.0.30.0090.01017.22
8.0.20.0100.01017.42
8.0.10.0040.00417.06
8.0.00.0170.00016.89
7.4.330.0000.00415.55
7.4.320.0000.00716.66
7.4.300.0030.00316.48
7.4.290.0060.00016.57
7.4.280.0040.00416.46
7.4.270.0030.00316.63
7.4.260.0030.00316.55
7.4.250.0000.00816.49
7.4.240.0040.00316.65
7.4.230.0000.00716.71
7.4.220.0040.01016.67
7.4.210.0070.00716.56
7.4.200.0000.00716.71
7.4.160.0100.00616.52
7.4.150.0120.00617.40
7.4.140.0130.00617.86
7.4.130.0050.01216.53
7.4.120.0120.01016.57
7.4.110.0130.01016.46
7.4.100.0130.00616.52
7.4.90.0100.00716.52
7.4.80.0080.01419.39
7.4.70.0120.00616.62
7.4.60.0030.01416.50
7.4.50.0060.00616.33
7.4.40.0070.01016.50
7.4.30.0140.00316.36
7.4.00.0060.00915.17
7.3.330.0000.00513.29
7.3.320.0030.00313.35
7.3.310.0000.00716.55
7.3.300.0000.00716.32
7.3.290.0070.00816.38
7.3.280.0110.00716.42
7.3.270.0140.00617.40
7.3.260.0150.00816.53
7.3.250.0110.01016.44
7.3.240.0110.00616.56
7.3.230.0120.00616.50
7.3.210.0140.00316.77
7.3.200.0110.01316.50
7.3.190.0130.00316.39
7.3.180.0090.01216.34
7.3.170.0120.00416.39
7.3.160.0120.00616.50
7.3.10.0100.00316.25
7.3.00.0100.01016.57
7.2.330.0140.00416.64
7.2.320.0110.01116.80
7.2.310.0120.00616.86
7.2.300.0120.00616.41
7.2.290.0090.01016.84
7.2.130.0120.00916.73
7.2.120.0160.00716.44
7.2.110.0000.01116.46
7.2.100.0110.00716.74
7.2.90.0140.01116.73
7.2.80.0040.01716.39
7.2.70.0120.00616.78
7.2.60.0120.00616.76
7.2.50.0100.00616.65
7.2.40.0170.00716.50
7.2.30.0120.01216.34
7.2.20.0080.01216.53
7.2.10.0100.01416.70
7.2.00.0040.02216.70
7.1.250.0000.01215.44
7.1.200.0040.00415.96
7.1.70.0080.00017.19
7.1.60.0060.00617.25
7.1.50.0070.00316.92
7.1.40.0090.00916.48
7.1.30.0040.00816.82
7.1.20.0000.01416.98
7.1.10.0000.00916.70
7.1.00.0030.00516.99
7.0.200.0030.00916.90
7.0.190.0030.00616.73
7.0.180.0030.00916.20
7.0.170.0080.00616.36
7.0.160.0070.01016.43
7.0.150.0000.01016.36
7.0.140.0070.00316.50
7.0.130.0030.00616.55
7.0.120.0000.00716.66
7.0.110.0060.00316.35
7.0.100.0000.00816.46
7.0.90.0050.00816.43
7.0.80.0070.00716.44
7.0.70.0070.01016.20
7.0.60.0030.01316.25
7.0.50.0040.00416.59
7.0.40.0050.00514.77
7.0.30.0000.00914.77
7.0.20.0040.00714.77
7.0.10.0030.00514.77
7.0.00.0040.00414.77

preferences:
64.18 ms | 400 KiB | 5 Q