3v4l.org

run code in 300+ PHP versions simultaneously
<? // A simple implementation of lossless compression and decompression using // Huffman coding. $asciiMap = array(); $symbolMap = array(); define('SYMBOL_SIZE', 8); for ($i = 0; $i < 256; ++$i) { $binary = decbin($i); while (strlen($binary) < SYMBOL_SIZE) $binary = "0$binary"; $asciiMap[chr($i)] = array_map(function($i) { return (int)$i; }, str_split($binary)); $symbolMap[$binary] = chr($i); } class Node { public $left; public $right; public $parent; public $symbol; public $weight; function __construct($symbol, $weight) { $this->left = null; $this->right = null; $this->parent = null; $this->symbol = $symbol; $this->weight = $weight; } function __toString() { return "Node ($this->symbol)"; } function priority() { return -$this->weight; } function dump($tabs) { $prefix = ""; for ($i = 0; $i < $tabs; ++$i) $prefix .= " "; print($prefix . "(" . $this->symbol . ", " . $this->weight . ")\n"); } } class Tree { public $root; public $leaves; function __construct() { $this->root = null; $this->leaves = array(); } function dump() { if (!$this->root) { print("empty"); return; } $tabs = 0; $q = array(); array_push($q, array($this->root, false)); while (count($q)) { $next = array_pop($q); if ($next[1]) { $tabs -= 1; continue; } $node = $next[0]; $node->dump($tabs); $tabs++; array_push($q, array($node, true)); if ($node->right) array_push($q, array($node->right, false)); if ($node->left) array_push($q, array($node->left, false)); } } } class PriorityQueue { public $heap; private $size; function __construct() { $this->heap = array(); $this->size = 0; } function size() { return $this->size; } function enqueue($e) { $this->heap[$this->size++] = $e; $this->bubbleUp(); } function bubbleUp() { $currentLocation = $this->size - 1; while ($currentLocation > 0) { $parentLocation = $currentLocation >> 1; $currentNode = $this->heap[$currentLocation]; $parentNode = $this->heap[$parentLocation]; if ($currentNode->priority() <= $parentNode->priority()) break; $this->heap[$parentLocation] = $currentNode; $this->heap[$currentLocation] = $parentNode; $currentLocation = $parentLocation; } } function dequeue() { if ($this->size() == 0) return null; $result = $this->heap[0]; $this->heap[0] = null; $this->filterDown(); $this->size--; return $result; } function filterDown() { if (!$this->size) return; $holeLocation = 0; $last = $this->heap[$this->size - 1]; while (true) { $child1Location = 2 * $holeLocation + 1; $child2Location = 2 * $holeLocation + 2; $child1 = null; $child2 = null; if ($child1Location < $this->size) $child1 = $this->heap[$child1Location]; if ($child2Location < $this->size) $child2 = $this->heap[$child2Location]; if (($child1 && $child2 && $child1->priority() > $child2->priority()) || ($child1 && !$child2)) { $this->heap[$holeLocation] = $child1; $this->heap[$child1Location] = null; $holeLocation = $child1Location; } else if ($child2) { $this->heap[$holeLocation] = $child2; $this->heap[$child2Location] = null; $holeLocation = $child2Location; } else { break; } } // We filtered down to the last element, so we don't need to do // anything else. if ($holeLocation == $this->size - 1) return; // We need to fill in the hole that we created in the middle of the // final level. $this->heap[$holeLocation] = $last; $this->heap[$this->size - 1] = null; } } class Huffman { public static function buildTree($s) { $t = new Tree(); $seenSymbols = array(); $pq = new PriorityQueue(); // Add leaf nodes for each symbol to the priority queue. for ($i = 0; $i < strlen($s); ++$i) { $c = $s[$i]; if (array_key_exists($c, $seenSymbols)) continue; $occurrences = 0; for ($j = $i; $j < strlen($s); ++$j) { if ($s[$j] != $c) continue; $occurrences++; } $node = new Node($c, $occurrences); $pq->enqueue($node); $t->leaves[$c] = $node; $seenSymbols[$c] = true; } if ($pq->size() == 0) return $t; // While there is more than one node left in the priority queue: while ($pq->size() > 1) { // Remove the two nodes of highest priority (lowest probability). $node1 = $pq->dequeue(); $node2 = $pq->dequeue(); // Create a new internal node with the two nodes as children with // p_total = p_1 + p_2 $newNode = new Node(null, $node1->weight + $node2->weight); $newNode->left = $node1; $newNode->right = $node2; $node1->parent = $newNode; $node2->parent = $newNode; // Add the new node to the queue. $pq->enqueue($newNode); } // The final node in the priority queue is the root. $t->root = $pq->dequeue(); return $t; } private static function encodeTree($t) { global $asciiMap; // Emit how big each leaf symbol is. // Emit a 0 for an internal node and a 1 for each leaf. $treeBits = array(); $q = array(); array_push($q, $t->root); while (count($q)) { $next = array_pop($q); if ($next->left && $next->right) { array_push($treeBits, 0); // Visit the left side first, so push the right side before // the left. array_push($q, $next->right); array_push($q, $next->left); } else { if ($next->left || $next->right) throw new Exception("Leaf nodes do not have children."); array_push($treeBits, 1); $symbolBits = $asciiMap[$next->symbol]; for ($i = 0; $i < count($symbolBits); ++$i) { array_push($treeBits, $symbolBits[$i]); } } } return $treeBits; } public static function encode($s, $t) { if (!$t->root) return; // Encode the tree first. $allBits = Huffman::encodeTree($t); for ($i = 0; $i < strlen($s); ++$i) { $c = $s[$i]; $current = $t->leaves[$c]; $bits = array(); while (true) { $parent = $current->parent; if (!$parent) break; if ($current === $parent->left) array_push($bits, 0); else array_push($bits, 1); $current = $parent; } $allBits = array_merge($allBits, array_reverse($bits)); } return $allBits; } private static function decodeTree($treeBits) { $t = new Tree(); if (!count($treeBits)) return $t; $leaves = array(); $helper = function($treeBits, $i) use (&$helper, &$leaves) { global $symbolMap; if ($treeBits[$i] == 1) { $i++; // TODO: Refactor Node so that it doesn't store the weight // internally. $symbolBits = array(); for ($j = 0; $j < SYMBOL_SIZE; ++$j) array_push($symbolBits, $treeBits[$i + $j]); $i += SYMBOL_SIZE; $symbol = $symbolMap[join("", $symbolBits)]; $node = new Node($symbol, 0); $leaves[$symbol] = $node; return array($i, $node); } else { $leftResult = $helper($treeBits, $i + 1); $leftChild = $leftResult[1]; $rightResult = $helper($treeBits, $leftResult[0]); $rightChild = $rightResult[1]; $node = new Node(null, 0); $node->left = $leftChild; $node->right = $rightChild; $leftChild->parent = $node; $rightChild->parent = $node; return array($rightResult[0], $node); } }; $result = $helper($treeBits, 0); $t->root = $result[1]; $t->leaves = $leaves; return array($result[0], $t); } public static function decode($bits) { $result = Huffman::decodeTree($bits); $startIndex = $result[0]; $tree = $result[1]; $current = $tree->root; $s = ""; $i = $startIndex; while ($i < count($bits)) { if ($current->left && $current->right) { if ($bits[$i++]) $current = $current->right; else $current = $current->left; } else { if ($current->left || $current->right) throw new Exception("Leaf must not have any children."); $s .= $current->symbol; $current = $tree->root; } } if ($current->left || $current->right) { throw new Exception("Leaf expected at end of input."); } $s .= $current->symbol; return $s; } } $s = join("\n", array( "4 Minute Warning - Radiohead", "", "This is just a nightmare", "Soon I'm gonna wake up", "Someone's gonna bring me 'round", "", "Running from the bombers", "Hiding in the forest", "Running through the fields", "Laying flat on the ground", "", "Just like everybody", "Stepping over hills", "Running from the underground", "", "This is your warning", "4 minute warning", "", "I don't wanna hear it", "I don't wanna know", "I just wanna run and hide", "", "This is just a nightmare", "But soon I'm gonna wake up", "Someone's gonna bring me 'round", "", "This is our warning", "4 minute warning" )); $RunHuffman = function() { global $s; $t = Huffman::buildTree($s); $bits = Huffman::encode($s, $t); $s2 = Huffman::decode($bits); if ($s2 != $s) { echo "Expected:\n$s\n"; echo "Actual:\n$s2\n"; throw new Exception("Incorrect result"); } }; $Huffman = new BenchmarkSuite('Huffman', [100000], [ new Benchmark('Huffman', true, false, 100000, $RunHuffman) ]); ?>

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.0170.00018.63
8.3.30.0070.00719.04
8.3.20.0000.00720.29
8.3.10.0080.00023.65
8.3.00.0070.00319.06
8.2.170.0110.00422.96
8.2.160.0080.00619.13
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0000.00726.16
8.2.120.0020.00522.26
8.2.110.0040.00820.29
8.2.100.0080.00417.72
8.2.90.0000.00818.03
8.2.80.0050.00317.97
8.2.70.0090.00017.38
8.2.60.0000.00817.92
8.2.50.0060.00318.07
8.2.40.0060.00319.95
8.2.30.0040.00418.01
8.2.20.0070.00017.57
8.2.10.0000.00717.95
8.2.00.0000.00817.71
8.1.270.0000.00821.95
8.1.260.0070.00026.35
8.1.250.0040.00428.09
8.1.240.0000.00922.20
8.1.230.0110.00020.97
8.1.220.0050.00317.74
8.1.210.0040.00418.91
8.1.200.0060.00317.10
8.1.190.0040.00416.98
8.1.180.0000.00718.10
8.1.170.0000.00818.30
8.1.160.0000.00721.89
8.1.150.0000.00718.70
8.1.140.0000.00717.38
8.1.130.0030.00317.65
8.1.120.0000.00817.42
8.1.110.0070.00017.33
8.1.100.0000.00717.35
8.1.90.0040.00417.26
8.1.80.0070.00017.31
8.1.70.0080.00017.35
8.1.60.0040.00417.48
8.1.50.0030.00617.45
8.1.40.0100.00317.41
8.1.30.0000.00817.59
8.1.20.0040.00417.47
8.1.10.0020.00517.45
8.1.00.0040.00417.26
8.0.300.0050.00320.11
8.0.290.0040.00416.63
8.0.280.0070.00018.32
8.0.270.0000.00717.25
8.0.260.0050.00317.21
8.0.250.0060.00016.82
8.0.240.0000.00616.86
8.0.230.0070.00016.80
8.0.220.0070.00016.78
8.0.210.0000.00716.80
8.0.200.0060.00016.83
8.0.190.0040.00416.75
8.0.180.0020.00516.79
8.0.170.0040.00416.80
8.0.160.0000.00716.82
8.0.150.0000.00716.65
8.0.140.0020.00516.63
8.0.130.0030.00313.65
8.0.120.0040.00416.70
8.0.110.0000.00816.79
8.0.100.0070.00016.80
8.0.90.0000.00716.65
8.0.80.0060.00916.77
8.0.70.0050.00216.83
8.0.60.0040.00416.75
8.0.50.0000.00816.75
8.0.30.0120.00917.06
8.0.20.0070.01117.40
8.0.10.0000.00716.97
8.0.00.0070.01016.79
7.4.330.0000.00615.09
7.4.320.0030.00316.52
7.4.300.0000.00716.34
7.4.290.0000.00716.51
7.4.280.0030.00516.28
7.4.270.0030.00316.37
7.4.260.0020.00516.47
7.4.250.0050.00316.45
7.4.240.0010.00616.45
7.4.230.0070.00016.23
7.4.220.0110.00716.43
7.4.210.0030.01016.45
7.4.200.0040.00416.31
7.4.160.0160.00016.44
7.4.150.0100.01017.40
7.4.140.0120.00617.86
7.4.130.0100.01116.42
7.4.120.0110.00716.43
7.4.110.0150.00316.50
7.4.100.0100.00716.29
7.4.90.0110.00716.24
7.4.80.0130.00419.39
7.4.70.0090.00916.44
7.4.60.0040.01416.34
7.4.50.0030.00516.25
7.4.40.0130.01016.19
7.4.30.0110.00516.27
7.4.00.0030.01414.54
7.3.330.0000.00613.68
7.3.320.0000.00713.61
7.3.310.0070.00016.13
7.3.300.0000.00716.19
7.3.290.0120.00816.25
7.3.280.0070.00916.13
7.3.270.0120.01217.40
7.3.260.0110.00516.30
7.3.250.0080.01316.15
7.3.240.0130.00316.14
7.3.230.0040.01316.42
7.3.210.0090.00616.24
7.3.200.0070.01619.39
7.3.190.0150.00616.15
7.3.180.0100.00716.39
7.3.170.0080.00816.61
7.3.160.0100.00616.60
7.2.330.0120.00516.59
7.2.320.0110.00516.50
7.2.310.0100.01316.37
7.2.300.0130.00316.66
7.2.290.0090.00616.46
7.2.60.0080.00816.49
7.2.00.0000.01419.58
7.1.200.0040.01115.60
7.1.100.0030.00917.96
7.1.70.0000.00817.24
7.1.60.0100.01719.32
7.1.50.0040.01816.90
7.1.00.0070.07322.44
7.0.200.0050.00516.70
7.0.140.0030.07321.99
7.0.100.0070.07020.39
7.0.90.0130.08720.23
7.0.80.0200.07720.08
7.0.70.0070.07720.09
7.0.60.0100.08720.18
7.0.50.0070.07020.51
7.0.40.0070.08020.13
7.0.30.0100.05020.10
7.0.20.0200.07320.14
7.0.10.0100.06720.13
7.0.00.0070.08020.04
5.6.280.0000.08021.12
5.6.250.0200.07321.15
5.6.240.0170.07320.93
5.6.230.0030.09320.84
5.6.220.0100.04721.04
5.6.210.0100.07720.98
5.6.200.0070.05021.29
5.6.190.0070.05321.27
5.6.180.0170.05021.34
5.6.170.0130.08321.43
5.6.160.0170.04321.44
5.6.150.0030.05321.44
5.6.140.0030.07321.35
5.6.130.0070.07321.46
5.6.120.0100.08721.37
5.6.110.0170.07721.34
5.6.100.0100.05721.39
5.6.90.0070.06021.26
5.6.80.0070.05020.77
5.6.70.0230.08020.76
5.6.60.0100.07720.73
5.6.50.0030.04320.66
5.6.40.0130.06720.62
5.6.30.0100.05320.83
5.6.20.0170.07320.69
5.6.10.0130.06720.69
5.6.00.0100.06720.84
5.5.380.0070.04720.75
5.5.370.0070.08320.88
5.5.360.0000.04720.68
5.5.350.0200.07720.68
5.5.340.0070.04721.27
5.5.330.0100.07321.14
5.5.320.0170.06021.16
5.5.310.0100.07021.25
5.5.300.0070.04021.21
5.5.290.0030.07720.98
5.5.280.0130.08321.11
5.5.270.0070.09021.22
5.5.260.0030.09321.21
5.5.250.0130.07721.02
5.5.240.0170.07720.52
5.5.230.0070.07020.55
5.5.220.0070.07320.61
5.5.210.0130.04320.45
5.5.200.0030.08720.49
5.5.190.0070.04020.52
5.5.180.0070.06720.41
5.5.160.0100.07320.59
5.5.150.0000.08720.44
5.5.140.0070.08020.31
5.5.130.0100.07720.50
5.5.120.0070.07020.60
5.5.110.0100.08020.54
5.5.100.0130.04720.32
5.5.90.0130.07020.40
5.5.80.0030.08720.41
5.5.70.0070.04320.47
5.5.60.0030.07020.27
5.5.50.0030.05020.39
5.5.40.0130.07320.50
5.5.30.0070.07720.29
5.5.20.0200.04020.43
5.5.10.0030.06020.30
5.5.00.0130.07320.39
5.4.450.0070.08319.61
5.4.440.0030.04319.63
5.4.430.0100.08319.69
5.4.420.0100.06319.71
5.4.410.0200.06719.42
5.4.400.0070.07319.33
5.4.390.0070.08019.12
5.4.380.0100.07319.16
5.4.370.0070.06319.32
5.4.360.0070.05019.30
5.4.350.0170.06719.39
5.4.340.0070.08019.37
5.4.320.0070.05719.29
5.4.310.0100.07319.32
5.4.300.0030.06319.29
5.4.290.0070.07719.46
5.4.280.0130.07319.32
5.4.270.0000.05319.33
5.4.260.0170.05719.45
5.4.250.0100.04019.29
5.4.240.0030.07319.38
5.4.230.0030.08019.36
5.4.220.0070.06719.35
5.4.210.0030.04719.47
5.4.200.0030.08019.11
5.4.190.0130.06319.29
5.4.180.0100.07319.16
5.4.170.0070.07019.15
5.4.160.0000.05019.35
5.4.150.0200.07019.46
5.4.140.0170.06716.72
5.4.130.0030.08016.60
5.4.120.0070.06716.55
5.4.110.0000.05016.74
5.4.100.0070.05016.60
5.4.90.0070.06316.78
5.4.80.0070.07016.66
5.4.70.0170.06716.57
5.4.60.0070.07316.59
5.4.50.0070.05716.62
5.4.40.0030.04716.79
5.4.30.0000.08016.63
5.4.20.0030.07016.63
5.4.10.0030.08016.60
5.4.00.0070.07016.02

preferences:
40.09 ms | 400 KiB | 5 Q