3v4l.org

run code in 300+ PHP versions simultaneously
<?php 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 getEncodingParts($input, &$output = [], &$nonBasicChars = []) { $isUtf8 = true; 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)) { $nonBasicChars[] = [$p, $codePoint, substr($input, $i, $len), $len]; // undecoded data is stored for UTF-8 chars to facilitate conversion to latin1 if necessary $i += $len; } else { // not a valid UTF-8 code point, convert $nonBasicChars to latin1 representation if (!$this->isPrintableLatin1Char($charCode)) { return false; } $isUtf8 = false; if (!empty($nonBasicChars)) { $offset = 0; for ($j = 0, $l = count($nonBasicChars); $j < $l; $j++) { $base = $nonBasicChars[$j][0]; $nonBasicChars[$j][0] += $offset; $nonBasicChars[$j][1] = ord($nonBasicChars[$j][2][0]); for ($k = 1; $k < $nonBasicChars[$j][3]; $k++) { $nonBasicChars[] = [$base + ++$offset, ord($nonBasicChars[$j][2][$k])]; } } $nonBasicChars[] = [$i++, $charCode]; usort($nonBasicChars, function($a, $b) { return $a[0] - $b[0]; }); } else { $nonBasicChars[] = [$i++, $charCode]; } } } else if ($this->isPrintableLatin1Char($charCode)) { $nonBasicChars[] = [$i++, $charCode]; } else { return false; } } else if ($this->isDnsLabelChar($charCode)) { $output[] = $input[$i++]; } else { return false; } } return true; } 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 ($digit >= 0x30 && $digit <= 0x39) { return $codePoint - 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; $i = 0; $bias = self::INITIAL_BIAS; for ($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); } $c = count($output) + 1; $bias = $this->adaptBias($i - $oldi, $c, $oldi === 0); $n += (int)($i / $c); $i %= $c; array_splice($output, $i, 0, [$this->encodeUtf8CodePoint($n)]); } return implode('', $output); } private function encodeLabel($input) { if (!$this->getEncodingParts($input, $output, $nonBasicChars)) { return false; } if (empty($nonBasicChars)) { return $input; } $output = 'xn--' . $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); } public function encode($input) { $output = []; foreach (explode('.', strtolower($input)) as $label) { if (false === $label = $this->encodeLabel($label)) { return false; } $output[] = $label; } return implode('.', $output); } } echo (new Punycode)->decode("xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n");

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.70.0130.00716.63
8.3.60.0160.00017.00
8.3.50.0090.00821.33
8.3.40.0130.00318.78
8.3.30.0180.00419.21
8.3.20.0040.00420.43
8.3.10.0050.00322.00
8.3.00.0050.00322.58
8.2.180.0120.00318.29
8.2.170.0150.00322.96
8.2.160.0090.00619.20
8.2.150.0000.00824.18
8.2.140.0070.00324.66
8.2.130.0040.01126.16
8.2.120.0030.00617.91
8.2.110.0060.00321.98
8.2.100.0080.00321.14
8.2.90.0000.00919.42
8.2.80.0000.00919.44
8.2.70.0000.00817.88
8.2.60.0030.00618.16
8.2.50.0030.00618.07
8.2.40.0030.00518.47
8.2.30.0000.00918.30
8.2.20.0000.00817.80
8.2.10.0000.00817.83
8.2.00.0030.00517.87
8.1.280.0070.01425.92
8.1.270.0040.00420.15
8.1.260.0050.00326.35
8.1.250.0000.00828.09
8.1.240.0110.00023.79
8.1.230.0040.00719.14
8.1.220.0000.00817.90
8.1.210.0030.00618.77
8.1.200.0040.00817.48
8.1.190.0050.00317.30
8.1.180.0030.00718.82
8.1.170.0050.00318.74
8.1.160.0040.00422.29
8.1.150.0000.00818.66
8.1.140.0050.00217.52
8.1.130.0000.01017.86
8.1.120.0040.00417.54
8.1.110.0040.00417.46
8.1.100.0050.00317.44
8.1.90.0040.00417.49
8.1.80.0000.00717.50
8.1.70.0000.00717.50
8.1.60.0050.00317.59
8.1.50.0030.00517.66
8.1.40.0030.00517.59
8.1.30.0030.00517.71
8.1.20.0000.00817.77
8.1.10.0000.00817.64
8.1.00.0000.00817.62
8.0.300.0000.00718.77
8.0.290.0040.00417.30
8.0.280.0070.00018.54
8.0.270.0050.00217.28
8.0.260.0070.00016.91
8.0.250.0000.00717.11
8.0.240.0060.00317.14
8.0.230.0030.00317.11
8.0.220.0000.00717.08
8.0.210.0040.00417.09
8.0.200.0000.00717.11
8.0.190.0040.00417.17
8.0.180.0040.00417.15
8.0.170.0040.00417.09
8.0.160.0030.00517.11
8.0.150.0000.00716.91
8.0.140.0080.00016.99
8.0.130.0000.00613.48
8.0.120.0070.00416.92
8.0.110.0020.00517.06
8.0.100.0000.00817.01
8.0.90.0030.00517.11
8.0.80.0030.01317.04
8.0.70.0040.00416.93
8.0.60.0040.00417.13
8.0.50.0000.00816.85
8.0.30.0070.01417.16
8.0.20.0160.00417.42
8.0.10.0020.00517.14
8.0.00.0120.01017.12
7.4.330.0000.00515.00
7.4.320.0000.00616.63
7.4.300.0030.00316.63
7.4.290.0050.00316.63
7.4.280.0070.00016.46
7.4.270.0000.00716.61
7.4.260.0000.00716.46
7.4.250.0030.00516.51
7.4.240.0020.00516.68
7.4.230.0030.00316.74
7.4.220.0150.00716.72
7.4.210.0060.01016.68
7.4.200.0030.00516.82
7.4.190.0000.00716.78
7.4.160.0090.00616.65
7.4.150.0060.01217.40
7.4.140.0100.01217.86
7.4.130.0110.01116.57
7.4.120.0080.00816.67
7.4.110.0060.01316.70
7.4.100.0090.00916.57
7.4.90.0120.00616.79
7.4.80.0100.01019.39
7.4.70.0110.00716.59
7.4.60.0070.01016.82
7.4.50.0000.00416.85
7.4.40.0060.00922.77
7.4.30.0070.01016.73
7.4.00.0030.00915.00
7.3.330.0000.00513.38
7.3.320.0060.00013.41
7.3.310.0030.00316.60
7.3.300.0030.00316.43
7.3.290.0060.01216.54
7.3.280.0130.00516.53
7.3.270.0070.01117.40
7.3.260.0130.01016.82
7.3.250.0120.00516.64
7.3.240.0120.00616.54
7.3.230.0090.00916.79
7.3.210.0070.01316.61
7.3.200.0190.00319.39
7.3.190.0160.00616.67
7.3.180.0000.01616.66
7.3.170.0030.01516.50
7.3.160.0140.00316.53
7.3.120.0090.00414.73
7.3.110.0120.00414.78
7.3.100.0030.01014.82
7.3.90.0090.00615.26
7.3.80.0070.00715.04
7.3.70.0060.00315.01
7.3.60.0100.00314.57
7.3.50.0030.00715.03
7.3.40.0060.00915.11
7.3.30.0030.00914.96
7.3.20.0060.00916.88
7.3.10.0080.00816.79
7.3.00.0070.00316.70
7.2.330.0070.01116.84
7.2.320.0080.01216.84
7.2.310.0070.01616.86
7.2.300.0030.01416.91
7.2.290.0110.00616.66
7.2.250.0060.01215.36
7.2.240.0060.01315.31
7.2.230.0000.01515.17
7.2.220.0040.00815.04
7.2.210.0070.01315.42
7.2.200.0080.00415.08
7.2.190.0030.01014.84
7.2.180.0000.01615.04
7.2.170.0100.00615.32
7.2.00.0060.00619.60
7.1.330.0080.00416.05
7.1.320.0070.00715.94
7.1.310.0060.00815.93
7.1.300.0000.00915.75
7.1.290.0120.00315.84
7.1.280.0090.00315.80
7.1.270.0140.00015.82
7.1.260.0120.00016.01
7.1.100.0090.00618.12
7.1.70.0040.00717.04
7.1.60.0070.01019.82
7.1.50.0090.01516.68
7.1.00.0000.03722.40
7.0.200.0030.00516.47
7.0.140.0030.07722.21
7.0.100.0370.08019.96
7.0.90.0270.04319.93
7.0.80.0370.07319.94
7.0.70.0370.07019.94
7.0.60.0470.07319.93
7.0.50.0330.08320.39
7.0.40.0100.08020.03
7.0.30.0130.07719.98
7.0.20.0130.07320.09
7.0.10.0100.08320.00
7.0.00.0070.08320.00
5.6.280.0030.07321.18
5.6.250.0170.06020.69
5.6.240.0200.07320.66
5.6.230.0070.08020.69
5.6.220.0070.08020.58
5.6.210.0100.07320.64
5.6.200.0100.08021.20
5.6.190.0130.08021.07
5.6.180.0100.08321.08
5.6.170.0070.06021.07
5.6.160.0170.07021.20
5.6.150.0100.07721.14
5.6.140.0130.07721.13
5.6.130.0200.07320.94
5.6.120.0130.07021.04
5.6.110.0130.08020.95
5.6.100.0130.07320.96
5.6.90.0130.07721.18
5.6.80.0100.06320.46
5.6.70.0030.08320.42
5.6.60.0070.08020.44
5.6.50.0030.08720.34
5.6.40.0070.05720.38
5.6.30.0100.07720.33
5.6.20.0030.08020.48
5.6.10.0200.06720.41
5.6.00.0070.07720.40
5.5.380.0100.07320.44
5.5.370.0130.07720.38
5.5.360.0100.06720.33
5.5.350.0030.07720.35
5.5.340.0130.07720.95
5.5.330.0030.09020.94
5.5.320.0130.07320.77
5.5.310.0030.09320.92
5.5.300.0130.07720.93
5.5.290.0070.08320.94
5.5.280.0130.08020.93
5.5.270.0070.08320.65
5.5.260.0030.08320.75
5.5.250.0100.04720.72
5.5.240.0000.07720.05
5.5.230.0000.09020.20
5.5.220.0100.04320.32
5.5.210.0100.07320.32
5.5.200.0130.06320.16
5.5.190.0100.08020.30
5.5.180.0130.07720.24
5.5.160.0000.06020.29
5.5.150.0000.08020.00
5.5.140.0100.06720.10
5.5.130.0070.08320.28
5.5.120.0130.06720.13
5.5.110.0070.07720.26
5.5.100.0070.07320.18
5.5.90.0100.07320.11
5.5.80.0070.08020.09
5.5.70.0100.07020.12
5.5.60.0030.08020.08
5.5.50.0100.07720.14
5.5.40.0070.08019.98
5.5.30.0070.08020.16
5.5.20.0070.08320.11
5.5.10.0070.08020.14
5.5.00.0070.07319.98
5.4.450.0100.07719.23
5.4.440.0070.08019.38
5.4.430.0070.04719.51
5.4.420.0100.07319.38
5.4.410.0100.07319.09
5.4.400.0170.06719.23
5.4.390.0100.07718.96
5.4.380.0100.07019.19
5.4.370.0070.08019.25
5.4.360.0130.07319.13
5.4.350.0030.07019.13
5.4.340.0070.05019.20
5.4.320.0100.07018.86
5.4.310.0070.08018.91
5.4.300.0000.08019.16
5.4.290.0030.07019.22
5.4.280.0100.07319.18
5.4.270.0100.07718.86
5.4.260.0070.08318.89
5.4.250.0100.07019.04
5.4.240.0100.06718.86
5.4.230.0130.06319.05
5.4.220.0030.07318.89
5.4.210.0100.06018.85
5.4.200.0030.08018.95
5.4.190.0070.05719.05
5.4.180.0070.07319.15
5.4.170.0030.08718.89
5.4.160.0000.08319.02
5.4.150.0130.07318.94
5.4.140.0070.06716.34
5.4.130.0030.07316.46
5.4.120.0070.07316.34
5.4.110.0070.07716.39
5.4.100.0070.06716.45
5.4.90.0070.05016.55
5.4.80.0070.07016.51
5.4.70.0070.07316.38
5.4.60.0130.06716.37
5.4.50.0100.07016.46
5.4.40.0130.06316.41
5.4.30.0100.04016.46
5.4.20.0070.07316.50
5.4.10.0030.07016.44
5.4.00.0100.07315.79
5.3.290.0030.07714.69
5.3.280.0100.07314.70
5.3.270.0100.07314.64
5.3.260.0000.08314.64
5.3.250.0100.07314.62
5.3.240.0070.07314.57
5.3.230.0130.05314.57
5.3.220.0030.08014.71
5.3.210.0170.06314.61
5.3.200.0170.04714.70
5.3.190.0070.07014.53
5.3.180.0000.07714.66
5.3.170.0100.07314.59
5.3.160.0100.07314.71
5.3.150.0030.08014.63
5.3.140.0070.07314.61
5.3.130.0100.07014.65
5.3.120.0100.06314.56
5.3.110.0070.07014.65
5.3.100.0030.06714.18
5.3.90.0100.07014.12
5.3.80.0030.07714.11
5.3.70.0100.03714.03
5.3.60.0030.07314.05
5.3.50.0000.08013.96
5.3.40.0070.06714.12
5.3.30.0070.05714.04
5.3.20.0170.06313.71
5.3.10.0030.07013.76
5.3.00.0070.07013.65

preferences:
46.15 ms | 401 KiB | 5 Q