3v4l.org

run code in 300+ PHP versions simultaneously
<?php <?php /** * Base62: A class to convert a number from any base between 2-62 to any other base between 2-62 * It doesn't use BC Math functions so works without the use of BC Math library. * It uses the native base_convert functions when the base is below 36 for faster execution. * The output number is backward compatible with the native base_convert function. * * Author : Lalit Patel * Website: http://www.lalit.org/lab/base62-php-convert-number-to-base-62-for-short-urls * License: Apache License 2.0 * http://www.apache.org/licenses/LICENSE-2.0 * Version: 0.1 (08 December 2011) * * Usage: * $converted_num = Base62::convert($number, $from_base, $to_base); */ class Base62 { /** * Converts a number/string from any base between 2-62 to any other base from 2-62 * @param mixed $number * @param integer $from_base * @param integer $to_base * @return $conveted_number. */ public static function convert($number, $from_base=10, $to_base=62) { if($to_base > 62 || $to_base < 2) { trigger_error("Invalid base (".he($to_base)."). Max base can be 62. Min base can be 2.", E_USER_ERROR); } //OPTIMIZATION: no need to convert 0 if("{$number}" === '0') { return 0; } //OPTIMIZATION: if to and from base are same. if($from_base == $to_base){ return $number; } //OPTIMIZATION: if base is lower than 36, use PHP internal function if($from_base <= 36 && $to_base <= 36) { // for lower base, use the default PHP function for faster results return base_convert($number, $from_base, $to_base); } // char list starts from 0-9 and then small alphabets and then capital alphabets // to make it compatible with eixisting base_convert function $charlist = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; if($from_base < $to_base) { // if converstion is from lower base to higher base // first get the number into decimal and then convert it to higher base from decimal; if($from_base != 10){ $decimal = self::convert($number, $from_base, 10); } else { $decimal = intval($number); } //get the list of valid characters $charlist = substr($charlist, 0, $to_base); if($number == 0) { return 0; } $converted = ''; while($number > 0) { $converted = $charlist{($number % $to_base)} . $converted; $number = floor($number / $to_base); } return $converted; } else { // if conversion is from higher base to lower base; // first convert it into decimal and the convert it to lower base with help of same function. $number = "{$number}"; $length = strlen($number); $decimal = 0; $i = 0; while($length > 0) { $char = $number{$length-1}; $pos = strpos($charlist, $char); if($pos === false){ trigger_error("Invalid character in the input number: ".($char), E_USER_ERROR); } $decimal += $pos * pow($from_base, $i); $length --; $i++; } return self::convert($decimal, 10, $to_base); } } } echo Base62::convert('53c15ee0c1899bd6478b459c', 16, 56); ?>

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)
5.4.300.0080.03912.50
5.4.290.0090.03912.50
5.4.280.0080.04012.39
5.4.270.0040.04012.39
5.4.260.0060.04012.39
5.4.250.0070.04212.39
5.4.240.0060.03912.39
5.4.230.0120.03612.38
5.4.220.0060.04112.38
5.4.210.0140.03412.38
5.4.200.0100.04012.38
5.4.190.0080.04012.38
5.4.180.0060.04212.38
5.4.170.0070.04012.38
5.4.160.0100.03812.38
5.4.150.0130.04612.38
5.4.140.0080.03912.07
5.4.130.0080.04412.05
5.4.120.0060.04112.02
5.4.110.0050.03912.01
5.4.100.0080.03612.01
5.4.90.0060.03812.01
5.4.80.0050.03912.01
5.4.70.0070.03412.00
5.4.60.0060.03512.01
5.4.50.0040.03812.01
5.4.40.0040.03812.00
5.4.30.0040.04011.99
5.4.20.0070.03511.99
5.4.10.0120.04311.99
5.4.00.0070.04011.48
5.3.280.0040.04412.71
5.3.270.0060.04212.72
5.3.260.0110.03612.72
5.3.250.0070.04412.72
5.3.240.0070.03912.72
5.3.230.0080.04212.71
5.3.220.0050.04212.68
5.3.210.0110.04012.68
5.3.200.0070.04212.68
5.3.190.0080.04312.68
5.3.180.0050.04512.67
5.3.170.0050.04212.66
5.3.160.0070.03912.68
5.3.150.0130.04912.68
5.3.140.0080.03812.66
5.3.130.0040.04312.66
5.3.120.0060.04212.66
5.3.110.0100.03812.65
5.3.100.0100.03812.12
5.3.90.0110.04812.09
5.3.80.0060.04012.07
5.3.70.0100.04912.07
5.3.60.0080.03912.06
5.3.50.0070.03912.00
5.3.40.0110.03712.00
5.3.30.0120.04511.95
5.3.20.0080.04811.73
5.3.10.0110.04511.70
5.3.00.0070.05011.68

preferences:
138.12 ms | 1394 KiB | 7 Q