3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TokenGenerator { const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; public function generateToken($length = 16) { $alphabetSize = strlen(self::ALPHABET); $token = ''; while (strlen($token) < $length) { $randomBytes = $this->generateRandomBytes(32); $offset = abs(hexdec(bin2hex(substr($randomBytes, 0, 4)))) % $alphabetSize; $token .= substr(self::ALPHABET, $offset, 1); } return $token; } private function generateRandomBytes($length) { $bytes = ''; // pre-fill with weak bytes while (strlen($bytes) < $length) { $bytes .= chr(mt_rand(0, 255)); } // merge with mcrypt iv if (function_exists('mcrypt_create_iv')) { $bytes = hash_hmac('sha256', $bytes, mcrypt_create_iv($length, MCRYPT_DEV_URANDOM), true); } // merge with openssl bytes if (function_exists('openssl_random_pseudo_bytes')) { $bytes = hash_hmac('sha256', $bytes, openssl_random_pseudo_bytes($length), true); } // merge with urandom if (file_exists('/dev/urandom') && is_readable('/dev/urandom')) { $bytes = hash_hmac('sha256', $bytes, file_get_contents('/dev/urandom', null, null, null, $length), true); } return substr($bytes, 0, $bytes); } } $gen = new TokenGenerator(); var_dump($gen->generateToken(10)); var_dump($gen->generateToken(10)); var_dump($gen->generateToken(10)); var_dump($gen->generateToken(20)); var_dump($gen->generateToken(20)); var_dump($gen->generateToken(20));

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)
7.1.00.0000.04022.31
7.0.140.0100.08022.04
5.6.280.0130.07720.91
5.4.280.0070.04312.39
5.4.270.0100.04812.39
5.4.260.0670.04719.35
5.4.250.0150.06412.39
5.4.240.0120.05212.39
5.4.230.0140.06312.38
5.4.220.0110.05812.38
5.4.210.0140.03312.38
5.4.200.0080.04112.38
5.4.190.0090.03712.38
5.4.180.0130.04512.38
5.4.170.0060.05212.39
5.4.160.0530.06319.29
5.4.150.0150.05012.38
5.4.140.0310.06812.06
5.4.130.0050.04512.05
5.4.120.0060.05012.01
5.4.110.0700.06016.42
5.4.100.0070.05412.00
5.4.90.0600.05016.36
5.4.80.0060.05112.00
5.4.70.0150.06112.00
5.4.60.0630.06716.53
5.4.50.0050.07512.00
5.4.40.0130.06011.99
5.4.30.0140.04911.98
5.4.20.0670.05016.45
5.4.10.0090.05911.98
5.4.00.0100.04911.47
5.3.280.0090.06512.71
5.3.270.0700.04314.66
5.3.260.0200.07312.71
5.3.250.0130.05912.72
5.3.240.0150.08112.72
5.3.230.0120.06412.71
5.3.220.0170.07812.68
5.3.210.0120.07912.68
5.3.200.0570.05714.62
5.3.190.0100.05812.68
5.3.180.0140.05712.68
5.3.170.0130.05612.67
5.3.160.0470.06314.57
5.3.150.0080.05812.68
5.3.140.0080.05712.66
5.3.130.0140.06512.66
5.3.120.0570.05714.57
5.3.110.0100.06612.66
5.3.100.0160.07712.12
5.3.90.0600.05314.03
5.3.80.0120.04912.08
5.3.70.0110.04712.07

preferences:
28.3 ms | 400 KiB | 5 Q