3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('random_bytes')) { /** * PHP 5.2.0 - 5.6.x way to implement random_bytes() * * In order of preference: * 1. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV) * 2. fread() /dev/arandom if available * 3. fread() /dev/urandom if available * 4. COM('CAPICOM.Utilities.1')->GetRandom() * 5. openssl_random_pseudo_bytes() */ if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7') >= 0) { /** * Powered by ext/mcrypt * * @param int $bytes * @return string */ function random_bytes($bytes) { if (!is_int($bytes)) { throw new Exception( 'Length must be an integer' ); } if ($bytes < 1) { throw new Exception( 'Length must be greater than 0' ); } // See PHP bug #55169 for why 5.3.7 is required $buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); if ($buf !== false) { if (RandomCompat_strlen($buf) === $bytes) { return $buf; } } /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } elseif (!ini_get('open_basedir') && (is_readable('/dev/arandom') || is_readable('/dev/urandom'))) { /** * Use /dev/arandom or /dev/urandom for random numbers * * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers * * @param int $bytes * @return string */ function random_bytes($bytes) { static $fp = null; if ($fp === null) { if (is_readable('/dev/arandom')) { $fp = fopen('/dev/arandom', 'rb'); } else { $fp = fopen('/dev/urandom', 'rb'); } } if ($fp !== false) { $streamset = stream_set_read_buffer($fp, 0); if ($streamset === 0) { $remaining = $bytes; $buf = ''; do { $read = fread($fp, $remaining); if ($read === false) { // We cannot safely read from urandom. $buf = false; break; } // Decrease the number of bytes returned from remaining $remaining -= RandomCompat_strlen($read); $buf .= $read; } while ($remaining > 0); if ($buf !== false) { if (RandomCompat_strlen($buf) === $bytes) { /** * Return our random entropy buffer here: */ return $buf; } } } } /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } elseif (extension_loaded('com_dotnet')) { /** * Windows with PHP < 5.3.0 will not have the function * openssl_random_pseudo_bytes() available, so let's use * CAPICOM to work around this deficiency. * * @param int $bytes * @return string */ function random_bytes($bytes) { $buf = ''; $util = new COM('CAPICOM.Utilities.1'); $execCount = 0; /** * Let's not let it loop forever. If we run N times and fail to * get N bytes of random data, then CAPICOM has failed us. */ do { $buf .= base64_decode($util->GetRandom($bytes, 0)); if (RandomCompat_strlen($buf) >= $bytes) { return RandomCompat_substr($buf, 0, $bytes); } ++$execCount; } while ($execCount < $bytes); /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } elseif (function_exists('openssl_random_pseudo_bytes')) { /** * Since openssl_random_pseudo_bytes() uses openssl's * RAND_pseudo_bytes() API, which has been marked as deprecated by the * OpenSSL team, this is our last resort before failure. * * @ref https://www.openssl.org/docs/crypto/RAND_bytes.html * * @param int $bytes * @return string */ function random_bytes($bytes) { $secure = true; $buf = openssl_random_pseudo_bytes($bytes, $secure); if ($buf !== false && $secure) { if (RandomCompat_strlen($buf) === $bytes) { return $buf; } } /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } else { /** * We don't have any more options, so let's throw an exception right now */ throw new Exception( 'There is no suitable CSPRNG installed on your system' ); } } var_dump( implode('-', [ bin2hex(random_bytes(4)), bin2hex(random_bytes(2)), bin2hex((random_bytes(1) & 0x0F) | 0x40) . bin2hex(random_bytes(1)), bin2hex((random_bytes(1) & 0x3F) | 0x80) . bin2hex(random_bytes(1)), bin2hex(random_bytes(12)) ]) );

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.0140.01025.92
8.3.50.0130.01325.92
8.3.40.0200.01725.92
8.3.30.0250.01325.92
8.3.20.0310.01125.92
8.3.10.0320.01025.92
8.3.00.0400.00625.92
8.2.180.0360.00725.92
8.2.170.0320.01025.92
8.2.160.0350.01025.92
8.2.150.0370.01125.92
8.2.140.0370.01425.92
8.2.130.0380.01025.92
8.2.120.0440.00725.92
8.2.110.0400.01025.92
8.2.100.0370.01625.92
8.2.90.0410.00625.92
8.2.80.0430.00425.92
8.2.70.0400.00625.92
8.2.60.0410.00625.92
8.2.50.0380.00825.92
8.2.40.0300.01325.92
8.2.30.0360.00925.92
8.2.20.0250.01625.92
8.2.10.0380.00725.92
8.2.00.0460.01725.92
8.1.280.0370.01425.92
8.1.270.0410.01025.92
8.1.260.0380.00925.92
8.1.250.0340.00625.92
8.1.240.0270.01525.92
8.1.230.0350.00625.92
8.1.220.0330.00925.92
8.1.210.0290.01025.92
8.1.200.0330.00325.92
8.1.190.0310.00425.92
8.1.180.0240.01225.92
8.1.170.0280.00925.92
8.1.160.0220.01425.92
8.1.150.0330.00725.92
8.1.140.0340.00925.92
8.1.130.0320.00925.92
8.1.120.0430.00925.92
8.1.110.0310.01425.92
8.1.100.0360.00725.92
8.1.90.0300.01225.92
8.1.80.0330.00925.92
8.1.70.0280.01425.92
8.1.60.0290.01925.92
8.1.50.0290.01825.92
8.1.40.0370.01125.92
8.1.30.0370.01025.92
8.1.20.0380.00825.92
8.1.10.0510.01025.92
8.1.00.0380.01025.92
8.0.300.0290.01525.92
8.0.290.0390.00325.92
8.0.280.0360.00725.92
8.0.270.0320.01025.92
8.0.260.0270.01425.92
8.0.250.0340.00725.92
8.0.240.0320.01125.92
8.0.230.0380.00725.92
8.0.220.0380.00325.92
8.0.210.0350.00825.92
8.0.200.0300.01225.92
8.0.190.0380.00325.92
8.0.180.0350.00725.92
8.0.170.0290.01225.92
8.0.160.0350.00625.92
8.0.150.0400.00425.92
8.0.140.0350.01425.92
8.0.130.0300.02225.92
8.0.120.0300.01125.92
8.0.110.0270.01425.92
8.0.100.0320.01125.92
8.0.90.0310.01125.92
8.0.80.0280.01425.92
8.0.70.0370.00425.92
8.0.60.0300.01925.92
8.0.50.0430.00625.92
8.0.30.0390.01225.92
8.0.20.0340.01125.92
8.0.10.0290.01125.92
8.0.00.0190.01125.92
7.4.330.0250.00925.92
7.4.320.0320.00825.92
7.4.300.0240.00425.92
7.4.290.0290.01025.92
7.4.280.0340.00025.92
7.4.270.0290.00425.92
7.4.260.0340.00625.92
7.4.250.0130.00317.49
7.4.240.0300.01025.92
7.4.230.0250.00925.92
7.4.220.0280.00825.92
7.4.210.0280.01025.92
7.4.200.0270.01125.92
7.4.190.0260.01125.92
7.4.180.0320.00925.92
7.4.160.0400.00025.92
7.4.150.0350.00325.92
7.4.140.0230.01525.92
7.4.130.0300.00825.92
7.4.120.0320.00325.92
7.4.110.0250.01125.92
7.4.100.0300.00625.92
7.4.90.0340.00325.92
7.4.80.0260.01125.92
7.4.70.0280.01025.92
7.4.60.0280.00925.92
7.4.50.0270.01225.92
7.4.40.0410.00325.92
7.4.30.0440.00325.92
7.4.20.0250.01225.92
7.4.10.0300.00725.92
7.4.00.0320.00625.92
7.3.330.0220.01625.92
7.3.320.0350.00325.92
7.3.310.0240.01425.92
7.3.300.0240.01225.92
7.3.290.0240.01225.92
7.3.280.0370.00025.92
7.3.270.0270.01025.92
7.3.260.0300.00725.92
7.3.250.0200.01625.92
7.3.240.0320.00625.92
7.3.230.0330.00425.92
7.3.220.0270.01125.92
7.3.210.0260.01225.92
7.3.200.0240.01225.92
7.3.190.0260.00625.92
7.3.180.0270.00925.92
7.3.170.0480.00425.92
7.3.160.0380.00625.92
7.3.150.0280.00725.92
7.3.140.0240.01025.92
7.3.130.0270.00725.92
7.3.120.0200.01225.92
7.3.110.0220.01525.92
7.3.100.0260.01125.92
7.3.90.0300.00725.92
7.3.80.0320.00625.92
7.3.70.0430.01025.92
7.3.60.0280.00825.92
7.3.50.0160.01120.46
7.3.40.0290.00425.92
7.3.30.0270.01025.92
7.3.20.0250.01225.92
7.3.10.0300.00725.92
7.3.00.0320.00625.92
7.2.340.0300.00625.92
7.2.330.0300.00825.92
7.2.320.0280.01025.92
7.2.310.0290.01025.92
7.2.300.0270.01025.92
7.2.290.0290.00825.92
7.2.280.0320.00625.92
7.2.270.0330.00725.92
7.2.260.0320.00625.92
7.2.250.0340.00325.92
7.2.240.0210.01225.92
7.2.230.0190.01525.92
7.2.220.0240.01225.92
7.2.210.0390.01025.92
7.2.200.0370.00325.92
7.2.190.0270.01225.92
7.2.180.0350.00325.92
7.2.170.0230.01025.92
7.2.160.0230.00325.92
7.2.150.0200.00725.92
7.2.140.0320.00625.92
7.2.130.0260.00625.92
7.2.120.0240.00325.92
7.2.110.0190.00825.92
7.2.100.0300.00725.92
7.2.90.0330.01125.92
7.2.80.0270.01525.92
7.2.70.0340.00325.92
7.2.60.0260.01125.92
7.2.50.0350.00325.92
7.2.40.0620.00921.91
7.2.30.0920.01022.11
7.2.20.0840.01121.82
7.2.10.1060.01222.03
7.2.00.0640.00921.15
7.1.330.0270.00725.92
7.1.320.0280.00625.92
7.1.310.0280.00625.92
7.1.300.0330.00025.92
7.1.290.0300.00325.92
7.1.280.0300.00325.92
7.1.270.0290.00625.92
7.1.260.0290.00625.92
7.1.250.0240.01425.92
7.1.240.0290.00325.92
7.1.230.0220.01225.92
7.1.220.0270.00725.92
7.1.210.0240.01425.92
7.1.200.0250.01125.92
7.1.190.0310.00925.92
7.1.180.0320.00625.92
7.1.170.0220.01125.92
7.1.160.1340.00621.39
7.1.150.0540.00521.36
7.1.140.0830.01521.38
7.1.130.0820.01321.38
7.1.120.0640.00621.25
7.1.110.0580.00521.15
7.1.100.0740.00720.91
7.1.90.0590.01021.03
7.1.80.0670.00821.02
7.1.70.0280.01019.49
7.1.60.0410.01226.19
7.1.50.0410.00925.14
7.1.40.0440.00929.17
7.1.30.0580.01029.28
7.1.20.0420.01629.32
7.1.10.0320.01120.33
7.1.00.0250.02921.13
7.0.330.0270.00325.92
7.0.320.0190.01525.92
7.0.310.0230.01225.92
7.0.300.0390.00325.92
7.0.290.1000.00721.21
7.0.280.0280.00625.92
7.0.270.0690.00921.23
7.0.260.0710.01121.05
7.0.250.0510.01220.93
7.0.240.0800.00920.89
7.0.230.0790.00520.92
7.0.220.0530.00720.92
7.0.210.0500.00520.42
7.0.200.0520.00819.07
7.0.190.0420.00820.26
7.0.180.0430.01020.28
7.0.170.0380.00720.05
7.0.160.1020.01420.25
7.0.150.0350.00820.11
7.0.140.0360.00720.07
7.0.130.0360.00620.26
7.0.120.0340.00620.25
7.0.110.0310.00920.22
7.0.100.0260.01620.08
7.0.90.0370.00620.13
7.0.80.0340.00520.11
7.0.70.0380.00720.14
7.0.60.0230.01620.11
7.0.50.0230.01719.51
7.0.40.0210.03620.13
7.0.30.0240.02520.34
7.0.20.0430.03620.29
7.0.10.0300.03020.26
7.0.00.0230.02220.21
5.6.400.0240.01025.92
5.6.390.0320.00425.92
5.6.380.0450.00325.92
5.6.370.0230.01025.92
5.6.360.0300.00825.92
5.6.350.0260.00425.92
5.6.340.0240.00725.92
5.6.330.0240.01025.92
5.6.320.0290.00425.92
5.6.310.0200.01425.92
5.6.300.0250.02723.46
5.6.290.0220.02923.44
5.6.280.0160.04522.67
5.6.270.0240.03123.46
5.6.260.0260.02923.41
5.6.250.0200.03923.37
5.6.240.0190.03523.36
5.6.230.0220.03923.34
5.6.220.0220.03223.41
5.6.210.0170.04822.49
5.6.200.0170.04021.65
5.6.190.0160.03622.49
5.6.180.0180.03722.37
5.6.170.0230.03822.39
5.6.160.0180.03922.52
5.6.150.0160.03521.67
5.6.140.0180.04421.72
5.6.130.0200.03521.61
5.6.120.0220.04522.67
5.6.110.0210.05222.59
5.6.100.0160.03722.60
5.6.90.0170.05022.61
5.6.80.0170.03322.24
5.6.70.0210.03523.12
5.6.60.0160.03523.02
5.6.50.0200.03423.13
5.6.40.0200.03023.13
5.6.30.0250.03623.01
5.6.20.0260.04423.01
5.6.10.0200.03823.10
5.6.00.0230.02823.01
5.5.380.0230.03721.72
5.5.370.0170.04121.67
5.5.360.0180.03321.72
5.5.350.1520.03521.31
5.5.340.0150.04920.54
5.5.330.0140.04821.42
5.5.320.0280.04921.44
5.5.310.0240.04521.47
5.5.300.0150.03720.64
5.5.290.0130.05020.64
5.5.280.0150.04121.62
5.5.270.0090.05621.52
5.5.260.0130.05621.58
5.5.250.0170.03021.50
5.5.240.0220.04621.24
5.5.230.0200.03321.71
5.5.220.0210.02821.68
5.5.210.0200.02921.71
5.5.200.0200.03121.65
5.5.190.0220.02821.63
5.5.180.0210.02821.63
5.5.170.0240.00825.92
5.5.160.0240.02521.66
5.5.150.0150.04221.66
5.5.140.0190.03221.53
5.5.130.0210.02721.61
5.5.120.0190.03021.60
5.5.110.0190.03021.49
5.5.100.0200.02921.44
5.5.90.0180.03021.56
5.5.80.0230.02521.61
5.5.70.0200.02921.48
5.5.60.0220.02721.58
5.5.50.0260.03021.47
5.5.40.0190.02921.65
5.5.30.0180.02821.59
5.5.20.0160.03221.47
5.5.10.0190.03121.60
5.5.00.0220.02821.61
5.4.450.0220.04221.52
5.4.440.0190.03921.55
5.4.430.0150.04121.57
5.4.420.0190.03721.57
5.4.410.0150.03721.55
5.4.400.0120.03821.44
5.4.390.0130.03821.30
5.4.380.0180.03321.32
5.4.370.0140.03821.45
5.4.360.0130.03921.40
5.4.350.0150.03721.47
5.4.340.0150.03521.37
5.4.330.0220.00425.92
5.4.320.0140.03821.31
5.4.310.0140.03821.37
5.4.300.0160.03421.29
5.4.290.0160.03921.40
5.4.280.0170.03421.41
5.4.270.0190.04321.32
5.4.260.0150.03921.32
5.4.250.0150.04621.33
5.4.240.0160.04221.29
5.4.230.0170.03621.34
5.4.220.0160.03721.39
5.4.210.0140.03421.29
5.4.200.0150.03420.61
5.4.190.0160.04121.28
5.4.180.0170.03321.34
5.4.170.0170.03521.28
5.4.160.0150.03821.36
5.4.150.0140.03521.34
5.4.140.0120.03819.78
5.4.130.0110.04119.62
5.4.120.0170.03019.76
5.4.110.0170.03619.65
5.4.100.0130.03519.64
5.4.90.0140.03719.67
5.4.80.0140.03719.70
5.4.70.0110.04119.70
5.4.60.0120.03819.59
5.4.50.0180.04419.62
5.4.40.0150.04319.67
5.4.30.0220.03019.61
5.4.20.0130.04219.61
5.4.10.0180.03219.72
5.4.00.0160.03619.29

preferences:
40.43 ms | 401 KiB | 5 Q