3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rb = new RBTree; $inputs = array(); for ($i = 0; $i <= 100; ++$i) { $key = mt_rand(); $value = mt_rand(); $rb->put($key, $value); $inputs[] = $key; } for ($i = 0; $i <= 100; ++$i) { $key = mt_rand(0, 100); var_dump($inputs[$key], $rb->get($inputs[$key])); echo "\n"; } var_dump(-1, $rb->get(-1)); class Node { public $color; public $key; public $value; public $left; public $right; public function __construct($key, $value, $color) { $this->key = $key; $this->value = $value; $this->color = $color; } public function compare($key) { if ($this->key < $key) return -1; elseif ($this->key > $key) return 1; else return 0; } } class RBTree { const RED = true; const BLACK = false; private $root; public function isEmpty() { return $this->root == null; } public function put($key, $value) { $this->root = $this->insert($this->root, (int)$key, $value); $this->root->color = self::BLACK; } public function get($key) { $x = $this->search($this->root, (int)$key); return $x ? $x->value : null; } private function isRed($h) { return $h ? $h->color : false; } private function insert($x, $key, $value) { if (!$x) return new Node($key, $value, self::RED); $cmp = $x->compare($key); if ($cmp > 0) $x->right = $this->insert($x->right, $key, $value); elseif ($cmp < 0) $x->left = $this->insert($x->left, $key, $value); else $x->value = $value; if ($this->isRed($x->right) && !$this->isRed($x->left)) $x = $this->rotateLeft($x); if ($this->isRed($x->left) && $this->isRed($x->left->left)) $x = $this->rotateRight($x); if ($this->isRed($x->left) && $this->isRed($x->right)) $this->flipColors($x); return $x; } private function search($x, $key) { while ($x) { $cmp = $x->compare($key); if ($cmp > 0) $x = $x->right; elseif ($cmp < 0) $x = $x->left; else return $x; } return null; } private function flipColors($h) { $h->color = !$h->color; $h->left->color = !$h->left->color; $h->right->color = $h->right->color; } private function rotateLeft($h) { $x = $h->right; $h->right = $x->left; $x->left = $h; $x->color = $x->left->color; $x->left->color = self::RED; return $x; } private function rotateRight($h) { $x = $h->left; $h->left = $x->right; $x->right = $h; $x->color = $x->right->color; $x->right->color = self::RED; return $x; } }

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)
7.4.00.0060.01615.01
7.3.120.0140.00714.96
7.3.110.0030.01514.70
7.3.100.0040.01914.80
7.3.90.0060.01115.05
7.3.80.0060.01014.91
7.3.70.0130.00615.01
7.3.60.0120.00614.66
7.3.50.0070.01014.98
7.3.40.0100.00715.08
7.3.30.0040.01414.63
7.3.20.0000.01416.66
7.3.10.0060.01216.48
7.3.00.0040.01816.45
7.2.250.0030.01714.83
7.2.240.0060.01315.34
7.2.230.0120.00615.23
7.2.220.0030.02114.93
7.2.210.0120.00615.41
7.2.200.0060.01215.39
7.2.190.0000.01615.17
7.2.180.0040.01615.23
7.2.170.0030.01615.32
7.2.130.0110.02116.68
7.2.120.0040.02116.55
7.2.110.0040.02216.65
7.2.100.0130.01916.44
7.2.90.0130.01316.67
7.2.80.0100.01616.89
7.2.70.0130.01316.61
7.2.60.0100.01716.48
7.2.50.0160.01316.72
7.2.40.0150.01816.78
7.2.30.0120.01216.57
7.2.20.0030.02116.35
7.2.10.0030.01416.27
7.2.00.0050.01218.09
7.1.330.0070.01315.59
7.1.320.0030.01515.79
7.1.310.0110.00615.91
7.1.300.0040.00815.92
7.1.290.0060.01315.66
7.1.280.0080.00415.71
7.1.270.0080.00415.78
7.1.260.0030.01315.81
7.1.250.0150.00615.54
7.1.70.0000.01216.95
7.1.60.0130.01619.29
7.1.50.0090.01217.00
7.1.00.0030.08022.29
7.0.200.0000.00816.69
7.0.140.0070.07321.97
7.0.110.0100.07321.82
7.0.100.0070.07321.71
7.0.90.0100.08321.68
7.0.80.0030.04721.76
7.0.70.0030.08321.68
7.0.60.0000.06721.65
7.0.50.0030.08021.79
7.0.40.0170.07719.61
7.0.30.0070.08019.80
7.0.20.0030.05719.79
7.0.10.0000.08719.69
7.0.00.0100.08319.68
5.6.280.0100.07721.27
5.6.260.0100.08020.84
5.6.250.0130.07720.72
5.6.240.0100.08720.71
5.6.230.0030.04320.63
5.6.220.0100.06020.69
5.6.210.0170.04320.84
5.6.200.0170.07720.78
5.6.190.0000.08720.62
5.6.180.0070.09320.77
5.6.170.0170.08320.79
5.6.160.0100.07020.77
5.6.150.0070.08320.72
5.6.140.0000.10020.79
5.6.130.0130.08020.78
5.6.120.0130.05020.69
5.6.110.0130.04720.76
5.6.100.0070.04320.79
5.6.90.0070.08320.82
5.6.80.0070.04020.05
5.6.70.0000.04720.14
5.6.60.0030.04320.14
5.6.50.0070.06020.18
5.6.40.0030.05320.20
5.6.30.0070.04720.17
5.6.20.0030.04320.16
5.6.10.0030.04720.04
5.6.00.0000.04720.43
5.5.380.0070.08320.70
5.5.370.0100.07720.69
5.5.360.0130.07720.68
5.5.350.0270.06720.57
5.5.340.0130.07321.15
5.5.330.0030.05721.18
5.5.320.0100.07720.83
5.5.310.0100.08321.07
5.5.300.0030.08721.03
5.5.290.0200.07321.06
5.5.280.0100.08721.02
5.5.270.0070.06720.93
5.5.260.0070.07321.00
5.5.250.0030.04320.83
5.5.240.0130.03320.44
5.5.230.0070.08020.38
5.5.220.0030.04720.52
5.5.210.0170.03020.30
5.5.200.0030.04720.19
5.5.190.0070.04020.35
5.5.180.0100.05320.21
5.5.160.0070.04720.39
5.5.150.0000.06020.43
5.5.140.0030.04720.29
5.5.130.0070.06320.43
5.5.120.0000.04720.25
5.5.110.0030.04320.54
5.5.100.0030.05320.14
5.5.90.0100.08320.33
5.5.80.0100.07320.18
5.5.70.0000.08720.39
5.5.60.0200.06720.22
5.5.50.0170.07020.36
5.5.40.0100.07720.20
5.5.30.0100.08020.38
5.5.20.0070.09020.15
5.5.10.0100.06720.32
5.5.00.0130.08320.14
5.4.450.0100.08019.39
5.4.440.0030.09319.43
5.4.430.0100.07719.42
5.4.420.0100.07319.78
5.4.410.0030.06019.66
5.4.400.0070.04019.19
5.4.390.0000.04719.33
5.4.380.0070.05319.09
5.4.370.0000.07319.29
5.4.360.0030.06319.18
5.4.350.0000.05719.21
5.4.340.0170.03719.10
5.4.320.0030.04319.07
5.4.310.0130.03719.46
5.4.300.0030.04319.06
5.4.290.0030.04319.35
5.4.280.0030.05019.46
5.4.270.0100.04319.09
5.4.260.0000.06719.17
5.4.250.0170.07019.10
5.4.240.0030.08719.11
5.4.230.0070.07719.04
5.4.220.0130.07719.08
5.4.210.0100.08719.36
5.4.200.0130.08019.16
5.4.190.0000.07019.11
5.4.180.0170.07019.13
5.4.170.0130.06319.20
5.4.160.0030.09019.43
5.4.150.0130.08019.16
5.4.140.0000.09316.52
5.4.130.0000.08316.44
5.4.120.0070.07716.42
5.4.110.0100.08316.69
5.4.100.0070.07716.70
5.4.90.0100.06716.50
5.4.80.0030.07716.55
5.4.70.0000.06316.56
5.4.60.0070.05016.53
5.4.50.0070.07716.63
5.4.40.0030.08016.40
5.4.30.0070.08316.61
5.4.20.0070.08316.54
5.4.10.0130.08716.63
5.4.00.0070.07315.96
5.3.290.0000.05014.84
5.3.280.0070.09014.88
5.3.270.0100.08314.76
5.3.260.0070.09014.78
5.3.250.0100.08014.77
5.3.240.0030.08314.89
5.3.230.0100.08014.61
5.3.220.0130.07314.86
5.3.210.0070.08714.75
5.3.200.0170.06314.68
5.3.190.0070.09014.80
5.3.180.0030.08714.95
5.3.170.0070.07314.80
5.3.160.0070.09014.71
5.3.150.0070.07714.73
5.3.140.0070.08714.67
5.3.130.0030.08314.65
5.3.120.0100.08014.71
5.3.110.0070.07014.83
5.3.100.0130.07314.06
5.3.90.0030.05314.30
5.3.80.0070.07714.15
5.3.70.0070.09014.40
5.3.60.0100.04314.38
5.3.50.0000.06013.95
5.3.40.0030.08314.09
5.3.30.0030.06314.07
5.3.20.0030.08313.85
5.3.10.0030.05013.80
5.3.00.0100.06013.80
5.2.170.0030.03711.63
5.2.160.0000.03711.63
5.2.150.0100.06011.75
5.2.140.0030.07711.63
5.2.130.0130.07711.63
5.2.120.0000.04011.63
5.2.110.0070.07011.63
5.2.100.0000.04711.63
5.2.90.0130.04011.63
5.2.80.0070.06711.63
5.2.70.0000.07711.67
5.2.60.0070.07011.63
5.2.50.0030.07011.63
5.2.40.0070.06711.63
5.2.30.0030.07711.63
5.2.20.0030.07011.63
5.2.10.0030.07711.63
5.2.00.0030.07711.63
5.1.60.0100.06311.63
5.1.50.0070.07011.63
5.1.40.0030.03711.63
5.1.30.0030.07311.63
5.1.20.0030.07011.63
5.1.10.0130.06011.63
5.1.00.0000.07011.63
5.0.50.0000.06711.63
5.0.40.0070.06311.63
5.0.30.0030.07711.63
5.0.20.0030.04311.63
5.0.10.0070.06311.63
5.0.00.0070.06011.63
4.4.90.0030.03311.63
4.4.80.0000.03311.63
4.4.70.0030.03311.63
4.4.60.0000.03311.63
4.4.50.0000.03311.63
4.4.40.0000.05711.63
4.4.30.0030.02711.63
4.4.20.0030.03711.63
4.4.10.0030.03311.63
4.4.00.0000.05711.63
4.3.110.0070.02711.63
4.3.100.0000.03311.63
4.3.90.0030.03311.63
4.3.80.0030.04311.63
4.3.70.0000.03311.63
4.3.60.0000.03711.63
4.3.50.0070.02711.63
4.3.40.0000.05311.63
4.3.30.0030.03311.63
4.3.20.0000.03711.63
4.3.10.0000.03711.63
4.3.00.0000.03711.63

preferences:
32.94 ms | 400 KiB | 5 Q