3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Katoga\Allyourbase; /** * @author Katoga <katoga.cz@hotmail.com> */ class Base32 implements Transcoder { /** * A-Z, 2-7 * New RFC that obsoleted RFC3548, uses the same alphabet. * * @var int */ const RFC4648 = 1; /** * 0-9, A-V * "Extended hex" or "base32hex" * * @var int */ const RFC2938 = 2; /** * 0-9, A-Z without I, L, O, U * * @link http://www.crockford.com/wrmg/base32.html * @var int */ const CROCKFORD = 3; /** * @var string */ const PAD_CHAR = '='; const ENCODE = 1; const DECODE = 1; /** * @var array */ protected $alphabet = [ self::RFC4648 => [ self::ENCODE => [], self::DECODE => [], ], self::RFC2938 => [ self::ENCODE => [], self::DECODE => [], ], self::CROCKFORD => [ self::ENCODE => [], self::DECODE => [], ], ]; /** * @param string $input binary string * @param int $type alphabet type * @return string ascii string */ public function encode($input, $type = self::RFC4648) { $output = ''; if ($input !== '') { $alphabet = $this->getAlphabet($type); // create binary represantation of input string $binStr = ''; foreach (str_split($input) as $char) { // append 8 bits of source string // padding zeros needed for chars with ASCII position < 64 (up to '?') // or portions of splitted multibyte chars $binStr .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT); } // pad binary string, its length has to be divisible by 5 $binStr = $this->pad($binStr, 5, '0'); // split binary string to 5bit chunks $binArr = explode(' ', trim(chunk_split($binStr, 5, ' '))); // encode foreach ($binArr as $binChar) { $output .= $alphabet[bindec($binChar)]; } // pad output, its length has to be divisible by 8 $output = $this->pad($output, 8, self::PAD_CHAR); } return $output; } /** * @param string $input ascii string * @param int $type alphabet type * @return string binary string * @throws DecodeFailedException */ public function decode($input, $type = self::RFC4648) { $output = ''; if ($input !== '') { $alphabet = $this->getDecodingAlphabet($type); // convert input to uppercase and remove trailing padding chars $input = rtrim(strtoupper($input), self::PAD_CHAR); $binStr = ''; foreach (str_split($input) as $ch) { if (!isset($alphabet[$ch])) { throw new DecodeFailedException(); } // append 5bit binary representation of encoded char $binStr .= str_pad((decbin($alphabet[$ch])), 5, '0', STR_PAD_LEFT); } // trim zeros from tight side of binary string, its length has to be divisible by 8 $binStr = $this->trim($binStr, 8, '0'); $binArr = explode(' ', trim(chunk_split($binStr, 8, ' '))); foreach ($binArr as $bin) { $output .= chr(bindec($bin)); } } return $output; } /** * Pads $string on right side with $char to length divisible by $factor * * @param string $string * @param int $factor * @param string $char */ protected function pad($string, $factor, $char) { $output = $string; $length = strlen($string); $modulo = $length % $factor; if ($modulo != 0) { $outputPaddedLength = $length + ($factor - $modulo); $output = str_pad($output, $outputPaddedLength, $char, STR_PAD_RIGHT); } return $output; } /** * Trims $char from right side of $string to length divisible by $factor * * @param string $string * @param int $factor * @param string $char */ protected function trim($string, $factor, $char) { $output = $string; $length = strlen($string); $modulo = $length % $factor; if ($modulo != 0) { $outputTrimmedLength = $length - $modulo; $output = substr($output, 0, $outputTrimmedLength); } return $output; } /** * @param int $type * @return array */ protected function getEncodingAlphabet($type) { return $this->alphabet[$type][self::ENCODE]; } /** * @param int $type * @return array */ protected function getDecodingAlphabet($type) { return $this->getAlphabet($type, self::DECODE); } /** * @param int $type * @param int $mode * @return array * @throws \InvalidArgumentException */ protected function getAlphabet($type, $mode) { if (!isset($this->alphabet[$type])) { throw new InvalidArgumentException(sprintf('Wrong alphabet requested: "%s"!', $type)); } if (!isset($this->alphabet[$type][$mode])) { throw new InvalidArgumentException(sprintf('Wrong mode requested: "%s"!', $mode)); } if (empty($this->alphabet[$type][$mode])) { // generate the requested alphabet switch ($type) { case self::RFC4648: $alphabet = array_merge( range('A', 'Z'), ['2', '3', '4', '5', '6', '7'] ); $this->alphabet[$type][self::ENCODE] = $alphabet; $this->alphabet[$type][self::DECODE] = array_flip($alphabet); break; case self::RFC2938: $alphabet = array_merge( ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], range('A', 'V') ); $this->alphabet[$type][self::ENCODE] = $alphabet; $this->alphabet[$type][self::DECODE] = array_flip($alphabet); break; case self::CROCKFORD: $alphabet = array_merge( ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], array_diff( range('A', 'Z'), ['I', 'L', 'O', 'U'] ) ); $this->alphabet[$type][self::ENCODE] = $alphabet; $decodeCrockford = array_merge( array_flip($alphabet), [ 'I' => 1, 'L' => 1, 'O' => 0 ] ); $lowercase = range('a', 'z'); unset($lowercase[20]); foreach ($lowercase as $ch) { $decodeCrockford[$ch] = $decodeCrockford[strtoupper($ch)]; } $this->alphabet[$type][self::DECODE] = $decodeCrockford; break; } } return $this->alphabet[$type][$mode]; } }

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.0120.00916.75
8.3.50.0110.00922.12
8.3.40.0070.00718.83
8.3.30.0090.00618.65
8.3.20.0050.00320.20
8.3.10.0030.00523.48
8.3.00.0040.00418.04
8.2.180.0090.00916.75
8.2.170.0120.00322.96
8.2.160.0070.00720.66
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0000.00726.16
8.2.120.0060.00317.75
8.2.110.0060.00622.27
8.2.100.0060.00618.05
8.2.90.0000.00919.35
8.2.80.0050.00317.97
8.2.70.0030.00517.48
8.2.60.0000.00818.16
8.2.50.0080.00018.07
8.2.40.0050.00321.06
8.2.30.0030.00720.67
8.2.20.0000.00917.75
8.2.10.0000.00918.04
8.2.00.0080.00018.07
8.1.280.0080.00825.92
8.1.270.0080.00023.92
8.1.260.0060.00326.35
8.1.250.0050.00228.09
8.1.240.0090.00022.53
8.1.230.0110.00017.48
8.1.220.0000.00817.77
8.1.210.0000.00818.77
8.1.200.0090.00017.25
8.1.190.0040.00417.35
8.1.180.0000.00918.10
8.1.170.0060.00318.74
8.1.160.0000.00722.07
8.1.150.0080.00018.92
8.1.140.0090.00019.16
8.1.130.0030.00517.80
8.1.120.0040.00417.49
8.1.110.0030.00717.39
8.1.100.0000.00817.53
8.1.90.0000.00817.52
8.1.80.0040.00717.47
8.1.70.0030.00517.48
8.1.60.0040.00417.66
8.1.50.0000.00917.52
8.1.40.0030.00617.44
8.1.30.0030.00617.71
8.1.20.0040.00417.55
8.1.10.0060.00317.59
8.1.00.0090.00317.47
8.0.300.0040.00418.77
8.0.290.0050.00216.88
8.0.280.0060.00318.48
8.0.270.0070.00017.27
8.0.260.0060.00017.26
8.0.250.0000.00917.12
8.0.240.0030.00317.10
8.0.230.0040.00417.00
8.0.220.0000.00816.96
8.0.210.0040.00417.12
8.0.200.0030.00616.98
8.0.190.0040.00416.96
8.0.180.0030.00516.96
8.0.170.0000.00816.98
8.0.160.0040.00417.04
8.0.150.0000.00716.95
8.0.140.0000.00716.97
8.0.130.0000.00613.41
8.0.120.0040.00417.05
8.0.110.0030.00516.94
8.0.100.0070.00016.92
8.0.90.0040.00417.12
8.0.80.0090.00617.08
8.0.70.0080.00017.03
8.0.60.0000.00816.93
8.0.50.0000.00816.96
8.0.30.0050.01417.14
8.0.20.0150.00717.40
8.0.10.0000.00816.98
8.0.00.0070.01116.77
7.4.330.0030.00315.08
7.4.320.0030.00316.64
7.4.300.0030.00316.61
7.4.290.0030.00316.59
7.4.280.0040.00416.42
7.4.270.0070.00016.64
7.4.260.0070.00016.50
7.4.250.0080.00016.52
7.4.240.0060.00216.46
7.4.230.0000.00716.43
7.4.220.0100.00916.63
7.4.210.0130.00616.54
7.4.200.0000.00716.73
7.4.160.0030.01216.72
7.4.150.0030.01417.40
7.4.140.0110.00917.86
7.4.130.0090.01016.60
7.4.120.0130.00716.53
7.4.110.0090.01216.57
7.4.100.0120.01316.60
7.4.90.0000.01716.67
7.4.80.0030.02119.39
7.4.70.0150.00316.57
7.4.60.0130.00916.61
7.4.50.0000.00516.52
7.4.40.0090.01516.49
7.4.30.0160.00916.56
7.4.10.0110.00714.68
7.4.00.0090.00914.90
7.3.330.0050.00013.29
7.3.320.0000.00513.25
7.3.310.0040.00416.34
7.3.300.0070.00016.38
7.3.290.0020.01216.28
7.3.280.0140.00416.37
7.3.270.0120.00617.40
7.3.260.0200.00616.68
7.3.250.0070.01216.44
7.3.240.0120.01216.36
7.3.230.0030.01316.39
7.3.210.0090.01316.48
7.3.200.0090.00616.67
7.3.190.0100.00616.31
7.3.180.0090.00916.43
7.3.170.0090.00616.32
7.3.160.0090.01216.31
7.3.130.0110.00714.93
7.3.120.0050.01214.73
7.3.110.0070.01014.88
7.3.100.0050.00814.81
7.3.90.0110.00514.82
7.3.80.0060.00414.80
7.3.70.0050.01014.69
7.3.60.0040.00714.60
7.3.50.0090.00614.54
7.3.40.0040.00914.76
7.3.30.0020.01114.70
7.3.20.0080.00616.65
7.3.10.0100.00316.54
7.3.00.0040.01016.56
7.2.330.0120.00616.73
7.2.320.0030.01516.62
7.2.310.0140.00316.54
7.2.300.0080.01616.53
7.2.290.0060.01216.34
7.2.260.0070.01114.97
7.2.250.0050.01014.88
7.2.240.0050.01214.75
7.2.230.0030.01014.97
7.2.220.0080.00814.93
7.2.210.0030.01014.99
7.2.200.0050.00714.84
7.2.190.0030.01215.03
7.2.180.0020.01514.86
7.2.170.0050.00514.80
7.2.160.0040.01115.17
7.2.150.0110.00416.86
7.2.140.0030.01016.84
7.2.130.0090.00316.84
7.2.120.0070.01116.78
7.2.110.0040.01116.87
7.2.100.0060.00316.80
7.2.90.0090.00316.80
7.2.80.0030.01416.60
7.2.70.0060.00617.00
7.2.60.0080.00516.72
7.2.50.0000.00916.84
7.2.40.0000.01316.57
7.2.30.0100.00016.85
7.2.20.0090.00616.49
7.2.10.0090.00916.73
7.2.00.0040.01318.13
7.1.330.0070.00715.83
7.1.320.0040.00715.74
7.1.310.0070.00715.58
7.1.300.0080.00615.71
7.1.290.0000.01515.65
7.1.280.0090.00215.38
7.1.270.0030.00715.52
7.1.260.0070.00815.66
7.1.250.0080.00615.74
7.1.240.0040.00715.63
7.1.230.0090.00615.62
7.1.220.0080.00415.61
7.1.210.0060.00615.69
7.1.200.0050.00715.60
7.1.190.0050.00515.63
7.1.180.0080.00515.60
7.1.170.0100.00315.27
7.1.160.0060.00615.69
7.1.150.0080.00815.59
7.1.140.0060.00315.82
7.1.130.0030.01015.70
7.1.120.0090.00615.71
7.1.110.0120.00615.72
7.1.100.0040.01016.54
7.1.90.0070.00715.40
7.1.80.0060.00515.47
7.1.70.0030.00716.30
7.1.60.0080.00917.51
7.1.50.0040.01116.18
7.1.40.0080.00415.59
7.1.30.0070.00715.71
7.1.20.0070.00415.68
7.1.10.0110.00415.73
7.1.00.0070.03818.91
7.0.330.0100.00315.23
7.0.320.0030.00915.14
7.0.310.0070.00015.24
7.0.300.0040.00415.16
7.0.290.0000.01115.20
7.0.280.0110.00415.46
7.0.270.0030.00715.05
7.0.260.0040.01115.23
7.0.250.0060.00315.11
7.0.240.0000.01215.28
7.0.230.0110.00315.41
7.0.220.0110.00015.11
7.0.210.0060.00615.33
7.0.200.0000.00915.88
7.0.190.0030.00815.21
7.0.180.0030.01015.33
7.0.170.0030.01015.06
7.0.160.0070.00415.43
7.0.150.0030.01215.29
7.0.140.0090.00615.41
7.0.130.0030.00915.16
7.0.120.0000.01315.39
7.0.110.0040.01115.14
7.0.100.0050.04317.71
7.0.90.0070.02217.58
7.0.80.0100.02017.77
7.0.70.0020.02717.77
7.0.60.0100.02417.56
7.0.50.0100.02317.92
7.0.40.0030.02816.79
7.0.30.0020.02916.63
7.0.20.0100.02016.66
7.0.10.0040.02416.81
7.0.00.0050.02516.69
5.6.400.0120.00314.34
5.6.390.0030.01014.18
5.6.380.0160.00014.29
5.6.370.0040.01114.37
5.6.360.0130.00314.38
5.6.350.0030.00914.26
5.6.340.0090.00614.00
5.6.330.0120.00013.88
5.6.320.0040.00814.18
5.6.310.0070.00713.75
5.6.300.0120.00014.13
5.6.290.0080.00614.20
5.6.280.0020.02417.44
5.6.270.0060.00914.37
5.6.260.0090.00914.21
5.6.250.0090.04517.62
5.6.240.0080.02217.37
5.6.230.0080.02817.58
5.6.220.0060.02217.25
5.6.210.0070.02317.49
5.6.200.0030.02517.77
5.6.190.0080.02717.70
5.6.180.0070.02717.65
5.6.170.0030.02417.55
5.6.160.0070.02217.86
5.6.150.0030.02517.81
5.6.140.0050.02117.55
5.6.130.0030.02517.56
5.6.120.0050.02817.60
5.6.110.0080.02117.58
5.6.100.0050.01817.59
5.6.90.0010.02617.63
5.6.80.0050.02017.19
5.6.70.0080.01517.25
5.6.60.0030.04017.19
5.6.50.0030.02517.32
5.6.40.0070.04417.44
5.6.30.0060.04017.20
5.6.20.0020.04417.20
5.6.10.0070.02217.43
5.6.00.0070.04017.28
5.5.380.0020.02617.22
5.5.370.0070.02717.19
5.5.360.0070.02317.12
5.5.350.0030.02517.21
5.5.340.0070.02417.58
5.5.330.0060.02117.63
5.5.320.0000.02617.38
5.5.310.0010.02917.47
5.5.300.0060.02417.54
5.5.290.0080.02317.54
5.5.280.0060.01817.43
5.5.270.0030.02617.49
5.5.260.0090.02217.52
5.5.250.0050.02017.44
5.5.240.0030.02517.24
5.5.230.0070.02317.17
5.5.220.0020.03217.31
5.5.210.0020.02817.11
5.5.200.0070.02517.27
5.5.190.0020.03717.23
5.5.180.0090.04217.10
5.5.170.0060.00314.27
5.5.160.0090.04117.14
5.5.150.0050.02717.10
5.5.140.0070.01817.01
5.5.130.0010.04817.23
5.5.120.0050.02217.12
5.5.110.0100.01717.03
5.5.100.0030.03217.08
5.5.90.0050.04117.02
5.5.80.0060.03216.94
5.5.70.0070.04217.13
5.5.60.0040.03117.12
5.5.50.0030.04117.00
5.5.40.0050.04217.07
5.5.30.0100.03817.10
5.5.20.0050.04017.01
5.5.10.0080.04017.10
5.5.00.0130.03817.08
5.4.450.0030.02115.21
5.4.440.0020.02715.16
5.4.430.0060.02515.21
5.4.420.0020.02815.18
5.4.410.0050.02414.84
5.4.400.0020.02214.80
5.4.390.0040.01915.14
5.4.380.0050.02015.03
5.4.370.0080.01515.00
5.4.360.0010.02114.89
5.4.350.0080.04014.89
5.4.340.0070.03214.91
5.4.330.0040.00410.95
5.4.320.0070.03714.99
5.4.310.0030.02115.08
5.4.300.0060.01714.94
5.4.290.0060.02415.03
5.4.280.0070.04214.90
5.4.270.0080.01815.01
5.4.260.0060.02115.00
5.4.250.0070.01714.84
5.4.240.0020.02414.91
5.4.230.0020.02315.02
5.4.220.0020.02614.83
5.4.210.0060.02615.13
5.4.200.0070.03614.86
5.4.190.0030.04615.05
5.4.180.0080.03814.92
5.4.170.0070.04314.93
5.4.160.0070.03314.93
5.4.150.0030.04614.75
5.4.140.0050.02213.74
5.4.130.0030.01913.73
5.4.120.0080.03913.61
5.4.110.0100.03813.62
5.4.100.0040.02913.71
5.4.90.0010.04013.56
5.4.80.0070.02213.76
5.4.70.0060.02113.72
5.4.60.0070.03013.72
5.4.50.0100.03013.62
5.4.40.0050.02413.70
5.4.30.0060.03513.67
5.4.20.0100.03113.70
5.4.10.0030.02913.70
5.4.00.0030.04013.43

preferences:
45.01 ms | 401 KiB | 5 Q