3v4l.org

run code in 300+ PHP versions simultaneously
<?php class LinkedList { /** @var LinkedListBucket */ public $first; /** @var LinkedListBucket */ public $last; public $length = 0; public $min = PHP_INT_MAX; public function append(LinkedListBucket $bucket) { if ($this->last) { $this->last->next = $bucket; } else { $this->first = $bucket; } $this->last = $bucket; $this->length++; if ($bucket->codePoint < $this->min) { $this->min = $bucket->codePoint; } } public function insert(LinkedListBucket $after, LinkedListBucket $new) { $new->next = $after->next; $after->next = $new; if ($after === $this->last) { $this->last = $new; } $this->length++; if ($new->codePoint < $this->min) { $this->min = $new->codePoint; } } } class LinkedListBucket { public $pos; public $codePoint; public $raw; public $rawLen; /** @var LinkedListBucket */ public $next; public function __construct($pos, $codePoint, $raw = null, $rawLen = 0) { $this->pos = $pos; $this->codePoint = $codePoint; $this->raw = $raw; $this->rawLen = $rawLen; } } class Punycode { const BASE = 36; const T_MIN = 1; const T_MAX = 26; const SKEW = 38; const DAMP = 700; const INITIAL_BIAS = 72; const INITIAL_N = 128; private function decodeUtf8Character($input, $i, &$codePoint = null) { $input = array_values(unpack('C*', substr($input, $i, 4))); $count = count($input); switch (true) { case $count >= 2 && ($input[0] & 0xE0) === 0xC0 && ($input[1] & 0xC0) === 0x80: $codePoint = (($input[0] & 0x1F) << 6) | ($input[1] & 0x3F); return 2; case $count >= 3 && ($input[0] & 0xF0) === 0xE0 && (($input[1] ^ 0x40) & ($input[2] ^ 0x40) & 0xC0) === 0xC0: $codePoint = (($input[0] & 0x0F) << 12) | (($input[1] & 0x3F) << 6) | ($input[2] & 0x3F); return 3; case $count >= 4 && ($input[0] & 0xF8) === 0xF0 && (($input[1] ^ 0x40) & ($input[2] ^ 0x40) & ($input[3] ^ 0x40) & 0xC0) === 0xC0: $codePoint = (($input[0] & 0x07) << 18) | (($input[1] & 0x3F) << 12) | (($input[2] & 0x3F) << 6) | ($input[3] & 0x3F); return 4; } return 0; } private function encodeUtf8CodePoint($codePoint) { switch (true) { case $codePoint < 0x80: return pack('C*', $codePoint & 0x7F); case $codePoint < 0x0800: return pack('C*', (($codePoint & 0x07C0) >> 6) | 0xC0, ($codePoint & 0x3F) | 0x80); case $codePoint < 0x010000: return pack('C*', (($codePoint & 0xF000) >> 12) | 0xE0, (($codePoint & 0x0FC0) >> 6) | 0x80, ($codePoint & 0x3F) | 0x80); case $codePoint < 0x110000: return pack('C*', (($codePoint & 0x1C0000) >> 18) | 0xF0, (($codePoint & 0x03F000) >> 12) | 0x80, (($codePoint & 0x0FC0) >> 6) | 0x80, ($codePoint & 0x3F) | 0x80); } return false; } private function isDnsLabelChar($charCode) { return ($charCode >= 0x61 && $charCode <= 0x7A) // lower-case letter || ($charCode >= 0x30 && $charCode <= 0x39) // digit || $charCode === 0x2D // - || ($charCode >= 0x41 && $charCode <= 0x5A); // upper-case letter; } private function isPrintableLatin1Char($charCode) { return $charCode >= 0xA0; } private function adaptBias($delta, $numPoints, $first) { $delta = $first ? $delta / self::DAMP : $delta / 2; $delta += $delta / $numPoints; $div = self::BASE - self::T_MIN; $end = ($div * self::T_MAX) / 2; for ($k = 0; $delta > $end; $k += self::BASE) { $delta = (int)($delta / $div); } return $k + (($div + 1) * $delta) / ($delta + self::SKEW); } private function decodeDigit($digits, $index) { if (isset($digits[$index])) { $codePoint = ord($digits[$index]); if ($codePoint >= 0x61 && $codePoint <= 0x7A) { return $codePoint - 97; } else if ($codePoint >= 0x30 && $codePoint <= 0x39) { return $codePoint - 22; } } return false; } private function encodeDigit($digit) { if ($digit <= 25) { return $digit + 97; } else if ($digit <= 35) { return $digit + 22; } return false; } private function decodeLabel($input) { if (substr($input, 0, 4) !== 'xn--') { return $input; } $input = substr($input, 4); if (false !== $nonBasicCharsStart = strrpos($input, '-')) { $output = str_split(substr($input, 0, $nonBasicCharsStart), 1); $nonBasicChars = substr($input, $nonBasicCharsStart + 1); } else { $output = []; $nonBasicChars = $input; } $n = self::INITIAL_N; $bias = self::INITIAL_BIAS; $c = count($output) + 1; for ($i = $j = 0, $l = strlen($nonBasicChars); $j < $l; $i++) { $oldi = $i; $w = 1; for ($k = self::BASE;; $k+= self::BASE) { if (false === $digit = $this->decodeDigit($nonBasicChars, $j++)) { return false; } $i += $digit * $w; if ($k <= $bias) { $t = self::T_MIN; } else if ($k >= $bias + self::T_MAX) { $t = self::T_MAX; } else { $t = $k - $bias; } if ($digit < $t) { break; } $w *= (int) (self::BASE - $t); } $bias = $this->adaptBias($i - $oldi, $c, $oldi === 0); $n += (int)($i / $c); $i %= $c++; echo "Insert $n at $i\n"; array_splice($output, $i, 0, [$this->encodeUtf8CodePoint($n)]); } return implode('', $output); } public function decode($input) { $output = []; foreach (explode('.', strtolower($input)) as $label) { if (false === $label = $this->decodeLabel($label)) { return false; } $output[] = $label; } return implode('.', $output); } private function getEncodingParts($input, &$output = null, &$nonBasicChars = null) { $isUtf8 = true; $output = []; $nonBasicChars = new LinkedList; for ($i = $p = 0, $l = strlen($input); $i < $l; $p++) { $charCode = ord($input[$i]); if ($charCode & 0x80) { if ($isUtf8) { if ($len = $this->decodeUtf8Character($input, $i, $codePoint)) { // undecoded data is stored for UTF-8 chars to facilitate conversion to latin1 if necessary $nonBasicChars->append(new LinkedListBucket($p, $codePoint, substr($input, $i, $len), $len)); $i += $len; } else { // not a valid UTF-8 code point, convert $nonBasicChars to latin1 representation if (!$this->isPrintableLatin1Char($charCode)) { return false; } $isUtf8 = false; if ($nonBasicChars->length > 0) { $offset = 0; $current = $nonBasicChars->first; do { $base = $current->pos; $current->pos += $offset; $current->codePoint = ord($current->raw[0]); for ($k = 1; $k < $current->rawLen; $k++) { $nonBasicChars->insert($current, new LinkedListBucket($base + ++$offset, ord($current->raw[$k]))); } } while($current = $current->next); } $nonBasicChars->append(new LinkedListBucket($i++, $charCode)); } } else if ($this->isPrintableLatin1Char($charCode)) { $nonBasicChars->append(new LinkedListBucket($i++, $charCode)); } else { return false; } } else if ($this->isDnsLabelChar($charCode)) { $output[] = $input[$i++]; } else { return false; } } return true; } private function encodeLabel($input) { /** @var LinkedList $nonBasicChars */ if (!$this->getEncodingParts($input, $output, $nonBasicChars)) { return false; } if ($nonBasicChars->length === 0) { return $input; } $n = self::INITIAL_N; $delta = 0; $bias = self::INITIAL_BIAS; $h = $b = count($output); if ($b > 0) { $output[] = '-'; } for ($l = $h + $nonBasicChars->length; $h < $l;) { $m = $nonBasicChars->min; $delta += ($m - $n) * ($h + 1); $n = $m; $current = $nonBasicChars->first; for ($i = $p = 0; $i < $l && $current; $i++) { if ($i === $current->pos) { $c = $current->codePoint; $current = $current->next; } else { $c = ord($output[$p++]); } if ($c < $n) { $delta++; } if ($c === $n) { $q = $delta; for ($k = self::BASE;; $k+= self::BASE) { if ($k <= $bias) { $t = self::T_MIN; } else if ($k >= $bias + self::T_MAX) { $t = self::T_MAX; } else { $t = $k - $bias; } if ($q < $t) { break; } $output[] = chr($this->encodeDigit($t + (($q - $t) % (self::BASE - $t)))); $q = ($q - $t) / (self::BASE - $t); } $output[] = chr($this->encodeDigit($q)); $bias = $this->adaptBias($delta, $h + 1, $h === $b); $delta = 0; $h++; } } $delta++; $n++; } return 'xn--' . implode('', $output); } public function encode($input) { $output = []; foreach (explode('.', strtolower($input)) as $label) { if (false === $label = $this->encodeLabel($label)) { return false; } $output[] = $label; } return implode('.', $output); } } $punycode = new Punycode; $input = "www.xn--ihqwcrb4cv8a8dqg056pqjye.com"; echo $punycode->decode($input);

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.0100.00618.55
8.3.50.0140.00721.98
8.3.40.0070.00719.00
8.3.30.0150.00319.39
8.3.20.0130.00620.29
8.3.10.0080.00022.14
8.3.00.0030.00522.57
8.2.180.0130.00316.75
8.2.170.0120.00422.96
8.2.160.0190.00319.46
8.2.150.0050.00324.18
8.2.140.0000.00824.66
8.2.130.0150.00326.16
8.2.120.0040.00419.69
8.2.110.0060.00322.20
8.2.100.0060.00618.47
8.2.90.0000.00819.34
8.2.80.0080.00018.16
8.2.70.0030.00617.75
8.2.60.0040.00418.17
8.2.50.0000.00818.07
8.2.40.0000.00818.28
8.2.30.0040.00418.16
8.2.20.0030.00617.86
8.2.10.0030.00517.90
8.2.00.0030.00617.84
8.1.280.0140.00425.92
8.1.270.0090.00018.73
8.1.260.0030.00526.35
8.1.250.0050.00328.09
8.1.240.0060.00323.79
8.1.230.0090.00319.24
8.1.220.0000.00817.91
8.1.210.0000.00818.77
8.1.200.0090.00017.48
8.1.190.0000.00917.55
8.1.180.0080.00020.48
8.1.170.0000.00918.67
8.1.160.0000.00722.36
8.1.150.0000.00818.79
8.1.140.0040.00417.61
8.1.130.0040.00417.86
8.1.120.0040.00417.63
8.1.110.0040.00417.61
8.1.100.0070.00017.48
8.1.90.0000.00817.58
8.1.80.0040.00417.51
8.1.70.0000.00717.61
8.1.60.0000.00817.64
8.1.50.0040.00417.63
8.1.40.0040.00417.68
8.1.30.0080.00017.80
8.1.20.0000.00817.72
8.1.10.0030.00617.59
8.1.00.0070.00417.68
8.0.300.0030.00518.77
8.0.290.0000.00717.30
8.0.280.0050.00318.64
8.0.270.0040.00417.34
8.0.260.0030.00316.95
8.0.250.0030.00317.19
8.0.240.0050.00317.19
8.0.230.0070.00017.04
8.0.220.0040.00417.10
8.0.210.0000.00717.06
8.0.200.0030.00617.07
8.0.190.0000.00917.19
8.0.180.0030.00616.97
8.0.170.0060.00317.12
8.0.160.0030.00616.93
8.0.150.0040.00417.06
8.0.140.0000.00817.09
8.0.130.0060.00013.61
8.0.120.0000.00817.00
8.0.110.0040.00417.11
8.0.100.0050.00316.94
8.0.90.0080.00016.94
8.0.80.0090.01517.16
8.0.70.0040.00417.00
8.0.60.0000.00817.09
8.0.50.0050.00317.06
8.0.30.0100.01017.29
8.0.20.0100.01217.40
8.0.10.0050.00317.09
8.0.00.0140.00817.03
7.4.330.0000.00515.00
7.4.320.0040.00416.69
7.4.300.0030.00316.77
7.4.290.0040.00416.80
7.4.280.0030.00616.51
7.4.270.0040.00416.74
7.4.260.0000.00716.75
7.4.250.0040.00416.64
7.4.240.0040.00416.63
7.4.230.0040.00416.79
7.4.220.0070.01416.61
7.4.210.0040.01316.63
7.4.200.0040.00416.79
7.4.190.0000.01016.71
7.4.160.0040.01416.69
7.4.150.0060.01217.40
7.4.140.0140.01017.86
7.4.130.0130.01316.67
7.4.120.0080.01016.61
7.4.110.0040.01716.64
7.4.100.0030.01616.68
7.4.90.0090.00916.70
7.4.80.0090.00919.39
7.4.70.0160.00316.52
7.4.60.0020.00516.60
7.4.50.0030.00616.64
7.4.40.0150.00022.77
7.4.30.0100.00716.60
7.4.10.0060.01215.02
7.4.00.0080.01015.13
7.3.330.0030.00313.64
7.3.320.0030.00313.39
7.3.310.0000.00716.42
7.3.300.0050.00316.45
7.3.290.0040.01116.58
7.3.280.0090.00716.55
7.3.270.0120.00617.40
7.3.260.0070.01116.66
7.3.250.0110.00716.62
7.3.240.0140.00716.71
7.3.230.0030.02016.75
7.3.210.0230.02416.62
7.3.200.0070.01019.39
7.3.190.0080.00916.63
7.3.180.0030.01716.84
7.3.170.0120.00316.63
7.3.160.0130.00616.57
7.3.130.0090.01014.90
7.3.120.0080.01015.05
7.3.110.0060.01214.91
7.3.100.0060.00614.95
7.3.90.0080.00715.05
7.3.80.0070.00715.00
7.3.70.0060.00914.99
7.3.60.0110.00514.98
7.3.50.0050.00914.91
7.3.40.0030.01114.98
7.3.30.0070.00914.97
7.3.20.0080.00316.75
7.3.10.0080.00716.47
7.3.00.0060.00816.43
7.2.330.0040.01516.62
7.2.320.0140.00616.93
7.2.310.0150.00316.62
7.2.300.0060.01016.57
7.2.290.0080.00816.89
7.2.260.0080.00915.15
7.2.250.0090.01015.27
7.2.240.0060.01215.21
7.2.230.0070.01115.16
7.2.220.0020.01015.07
7.2.210.0060.01115.17
7.2.200.0070.00915.20
7.2.190.0050.00915.14
7.2.180.0060.00915.09
7.2.170.0070.00915.26
7.2.160.0060.00814.99
7.2.150.0030.01217.04
7.2.140.0020.01316.81
7.2.130.0080.00716.84
7.2.120.0070.00816.93
7.2.110.0090.00716.68
7.2.100.0060.01016.75
7.2.90.0080.00816.81
7.2.80.0050.00916.82
7.2.70.0100.00716.81
7.2.60.0110.00516.86
7.2.50.0090.01016.86
7.2.40.0070.01116.93
7.2.30.0110.00716.78
7.2.20.0070.01116.70
7.2.10.0120.00716.88
7.2.00.0050.01217.52
7.1.330.0080.00615.90
7.1.320.0040.00915.81
7.1.310.0050.00815.82
7.1.300.0030.00815.87
7.1.290.0050.01015.68
7.1.280.0100.00615.86
7.1.270.0060.00715.74
7.1.260.0100.00615.81
7.1.250.0060.00815.47
7.1.240.0030.01015.87
7.1.230.0050.01215.81
7.1.220.0050.01015.67
7.1.210.0040.01015.70
7.1.200.0070.00715.87
7.1.190.0080.00815.85
7.1.180.0030.01115.80
7.1.170.0020.01215.82
7.1.160.0080.01015.74
7.1.150.0060.00715.71
7.1.140.0070.00615.65
7.1.130.0050.00915.85
7.1.120.0060.01015.72
7.1.110.0070.00815.76
7.1.100.0020.00916.75
7.1.90.0080.00715.64
7.1.80.0090.00315.76
7.1.70.0050.00816.17
7.1.60.0100.00817.02
7.1.50.0060.01116.21
7.1.40.0080.00315.80
7.1.30.0110.00315.70
7.1.20.0040.00815.82
7.1.10.0050.01315.80
7.1.00.0030.03417.94
7.0.330.0040.00615.33
7.0.320.0080.00815.47
7.0.310.0000.01515.38
7.0.300.0060.00815.38
7.0.290.0080.00315.43
7.0.280.0050.00915.43
7.0.270.0050.00715.45
7.0.260.0050.00915.38
7.0.250.0060.00615.58
7.0.240.0030.01215.52
7.0.230.0060.00315.44
7.0.220.0020.01115.44
7.0.210.0070.00815.37
7.0.200.0140.00714.94
7.0.190.0050.01015.26
7.0.180.0040.00915.42
7.0.170.0060.00615.58
7.0.160.0050.00715.42
7.0.150.0040.00715.33
7.0.140.0050.03017.60
7.0.130.0080.00315.53
7.0.120.0030.00915.45
7.0.110.0090.00515.53
7.0.100.0060.02816.96
7.0.90.0070.03016.89
7.0.80.0070.01616.96
7.0.70.0110.02816.95
7.0.60.0060.02816.86
7.0.50.0140.03017.07
7.0.40.0070.03015.62
7.0.30.0090.02815.75
7.0.20.0090.01615.60
7.0.10.0080.02415.67
7.0.00.0100.01515.65
5.6.400.0080.00814.71
5.6.390.0060.00914.46
5.6.380.0080.00914.46
5.6.370.0040.00914.35
5.6.360.0070.00914.62
5.6.350.0030.01114.53
5.6.340.0080.00614.63
5.6.330.0050.00714.63
5.6.320.0050.01014.60
5.6.310.0040.00914.71
5.6.300.0070.00714.44
5.6.290.0060.00614.55
5.6.280.0060.02916.75
5.6.270.0070.00714.61
5.6.260.0050.00714.62
5.6.250.0060.03216.51
5.6.240.0050.03016.63
5.6.230.0060.03416.50
5.6.220.0050.03416.53
5.6.210.0070.03116.50
5.6.200.0090.01916.65
5.6.190.0050.02916.69
5.6.180.0070.02816.81
5.6.170.0070.02616.68
5.6.160.0060.03216.70
5.6.150.0020.02216.67
5.6.140.0040.01916.71
5.6.130.0080.02316.83
5.6.120.0020.03616.65
5.6.110.0060.01716.67
5.6.100.0050.02816.71
5.6.90.0060.02916.81
5.6.80.0030.03516.38
5.6.70.0060.03016.47
5.6.60.0010.02916.48
5.6.50.0020.03516.39
5.6.40.0070.03016.49
5.6.30.0040.03216.62
5.6.20.0010.02816.40
5.6.10.0130.02416.51
5.6.00.0080.03016.42
5.5.380.0080.03116.49
5.5.370.0090.01916.45
5.5.360.0090.03216.39
5.5.350.0070.02116.40
5.5.340.0030.02816.64
5.5.330.0050.03516.60
5.5.320.0020.02416.52
5.5.310.0010.02216.58
5.5.300.0050.03316.61
5.5.290.0080.03116.76
5.5.280.0060.02816.58
5.5.270.0070.03216.55
5.5.260.0060.03116.55
5.5.250.0050.02016.50
5.5.240.0120.02716.29
5.5.230.0050.02716.31
5.5.220.0080.01816.36
5.5.210.0060.03516.32
5.5.200.0060.03116.42
5.5.190.0090.02816.25
5.5.180.0090.02516.20
5.5.170.0040.01214.52
5.5.160.0050.03116.28
5.5.150.0060.03416.30
5.5.140.0050.02716.24
5.5.130.0080.03216.36
5.5.120.0070.02816.26
5.5.110.0060.03216.18
5.5.100.0150.01116.33
5.5.90.0100.02816.25
5.5.80.0050.03316.42
5.5.70.0050.01916.38
5.5.60.0030.02116.31
5.5.50.0050.02016.47
5.5.40.0060.01916.35
5.5.30.0060.01916.44
5.5.20.0050.01916.42
5.5.10.0030.02016.24
5.5.00.0100.01216.34
5.4.450.0050.02614.14
5.4.440.0030.01814.13
5.4.430.0040.02114.01
5.4.420.0030.02114.07
5.4.410.0030.03214.05
5.4.400.0060.02613.98
5.4.390.0030.02813.87
5.4.380.0050.01813.95
5.4.370.0050.03213.96
5.4.360.0050.01813.95
5.4.350.0090.02713.98
5.4.340.0080.02913.84
5.4.330.0090.00511.28
5.4.320.0070.02613.85
5.4.310.0030.02213.93
5.4.300.0060.02713.95
5.4.290.0070.02513.98
5.4.280.0090.01313.93
5.4.270.0050.02913.86
5.4.260.0060.03113.89
5.4.250.0050.02813.98
5.4.240.0050.01913.93
5.4.230.0070.03114.00
5.4.220.0030.01713.96
5.4.210.0060.01713.86
5.4.200.0100.01413.90
5.4.190.0010.01913.80
5.4.180.0070.01513.96
5.4.170.0050.01813.93
5.4.160.0070.01713.90
5.4.150.0030.02013.99
5.4.140.0040.01513.15
5.4.130.0040.01613.13
5.4.120.0030.01912.99
5.4.110.0060.01613.10
5.4.100.0080.01213.09
5.4.90.0000.01813.08
5.4.80.0060.01413.04
5.4.70.0100.01312.97
5.4.60.0020.01813.00
5.4.50.0070.02013.08
5.4.40.0050.01713.03
5.4.30.0050.01813.00
5.4.20.0040.01813.10
5.4.10.0060.01613.08
5.4.00.0060.01212.82
5.3.290.0050.02612.16
5.3.280.0070.01612.17
5.3.270.0050.01712.22
5.3.260.0080.01312.23
5.3.250.0080.01312.18
5.3.240.0050.01612.17
5.3.230.0040.01712.20
5.3.220.0010.02212.13
5.3.210.0040.01512.13
5.3.200.0050.01312.12
5.3.190.0040.01912.13
5.3.180.0030.01712.25
5.3.170.0030.01712.26
5.3.160.0030.01912.29
5.3.150.0020.01912.16
5.3.140.0030.01712.20
5.3.130.0030.01712.13
5.3.120.0050.01812.21
5.3.110.0090.01212.26
5.3.100.0050.01512.04
5.3.90.0050.01412.02
5.3.80.0060.01612.00
5.3.70.0040.01611.99
5.3.60.0020.01512.06
5.3.50.0050.01312.00
5.3.40.0010.01711.89
5.3.30.0020.01511.88
5.3.20.0010.01711.94
5.3.10.0040.01211.87
5.3.00.0040.01511.86

preferences:
46.85 ms | 401 KiB | 5 Q