3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('random_bytes')) { function random_bytes($size) { if(!is_int($size)) throw new InvalidArgumentException('random_bytes: $size must be an int'); if($size < 0) throw new InvalidArgumentException('random_bytes: $size must not be negative'); //if(function_exists('mcrypt_create_iv')) //{ // $result = mcrypt_create_iv($size, MCRYPT_DEV_URANDOM); //} else if(function_exists('openssl_random_pseudo_bytes')) { $result = openssl_random_pseudo_bytes($size, $isSecure); if($isSecure !== true) { throw new RuntimeException("random_bytes: openssl_random_pseudo_bytes returned insecure data"); } } else { throw new RuntimeException("random_bytes: No RNG found"); } if(!is_string($result) || (strlen($result) !== $size)) { throw new RuntimeException("random_bytes: RNG is unavailable or broken"); } return $result; } } if (!function_exists('random_int')) { function random_int($min, $max) { if(!defined('PHP_INT_SIZE')) trigger_error("random_int: This version of PHP is not supported", E_USER_ERROR); if(!is_int($min)) throw new InvalidArgumentException('random_int: $min must be an int'); if(!is_int($min)) throw new InvalidArgumentException('random_int: $min must be an int'); if($min > $max) throw new InvalidArgumentException('random_int: $min must be less or equal to $max'); $range = $max - $min + 1; // the rejection probability is at most 0.5, so this corresponds to a failure probability of 2^-128 for a working RNG for($attempts = 0; $attempts < 128; $attempts++) { // generate a random integer $bytes = random_bytes(PHP_INT_SIZE); $value = 0; for($i = 0; $i < PHP_INT_SIZE; $i++) { $value = ($value << 8) | ord($bytes[$i]); } if(!is_int($range)) { if(($value >= $min) && ($value <= $max)) { return $value; } } else { $value &= PHP_INT_MAX; // equivalent to (PHP_INT_MAX + 1) % range, but avoids int overflows // I'm assuming PHP_INT_MAX + 1 is a power-of-two $reject = (-$range & PHP_INT_MAX) % $range; if($value >= $reject) { return ($value % $range) + $min; } } } throw new RuntimeException("random_int: RNG is broken - too many rejections"); } } random_int(0, 1000);

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.4.00.0040.01414.66
7.3.120.0070.01014.70
7.3.110.0110.00714.66
7.3.100.0030.01214.79
7.3.90.0060.01014.54
7.3.80.0070.00715.01
7.3.70.0090.00914.83
7.3.60.0040.01114.54
7.3.50.0030.01014.79
7.3.40.0040.01114.83
7.3.30.0100.00014.48
7.3.20.0070.00716.61
7.3.10.0090.00616.66
7.3.00.0000.01016.66
7.2.250.0060.00914.83
7.2.240.0060.00914.88
7.2.230.0110.00715.10
7.2.220.0030.01014.78
7.2.210.0000.01014.93
7.2.200.0040.01115.04
7.2.190.0100.00714.67
7.2.180.0070.00714.84
7.2.170.0030.01314.57
7.1.330.0030.01015.51
7.1.320.0090.00615.49
7.1.310.0070.00715.70
7.1.300.0110.00415.59
7.1.290.0030.01015.72
7.1.280.0030.01215.68
7.1.270.0030.01015.56
7.1.260.0040.00415.57
7.1.70.0030.00316.91
7.1.60.0090.01619.48
7.1.50.0090.00916.79
7.1.00.0070.07722.26
7.0.200.0140.00316.85
7.0.60.0130.07019.98
7.0.50.0130.08017.83
7.0.40.0070.08020.13
7.0.30.0230.05020.11
7.0.20.0130.09320.30
7.0.10.0330.07320.05
7.0.00.0100.06020.21
5.6.280.0030.07320.93
5.6.210.0100.05320.78
5.6.200.0130.07318.36
5.6.190.0130.08020.50
5.6.180.3400.04320.46
5.6.170.0300.06320.57
5.6.160.0100.07320.54
5.6.150.0000.04018.21
5.6.140.0070.08318.25
5.6.130.0100.06318.27
5.6.120.0130.03721.16
5.6.110.0030.05321.06
5.6.100.0070.08721.16
5.6.90.0030.05721.05
5.6.80.0100.07720.30
5.6.70.0100.07720.41
5.5.350.0270.07020.45
5.5.340.0170.07018.04
5.5.330.0030.06020.27
5.5.320.0270.04720.37
5.5.310.0270.04020.24
5.5.300.0000.09017.97
5.5.290.0000.04318.05
5.5.280.0130.07020.85
5.5.270.0070.08320.69
5.5.260.0030.08720.96
5.5.250.0070.05720.66
5.5.240.0370.06320.27
5.4.450.0570.07319.48
5.4.440.0730.05719.63
5.4.430.0170.05319.43
5.4.420.0030.06319.51
5.4.410.0130.05319.52
5.4.400.0030.06019.22
5.4.390.0070.05719.25
5.4.380.0130.05019.08
5.4.370.0030.06719.09
5.4.360.0170.05319.27
5.4.350.0130.05019.16
5.4.340.0070.05719.11
5.4.320.0000.06319.15
5.4.310.0030.06319.06
5.4.300.0070.05719.26
5.4.290.0100.05019.07
5.4.280.0100.05019.15
5.4.270.0100.05019.11
5.4.260.0100.05019.06
5.4.250.0070.05719.06
5.4.240.0100.05719.16
5.4.230.0100.07019.08
5.4.220.0130.05319.07
5.4.210.0270.05319.23
5.4.200.0130.04716.86
5.4.190.0100.05319.03
5.4.180.0130.05319.07
5.4.170.0230.06319.06
5.4.160.0030.05719.11
5.4.150.0130.04719.13
5.4.140.0100.05016.65
5.4.130.0070.05016.58
5.4.120.0030.05316.42
5.4.110.0100.04716.54
5.4.100.0100.05016.48
5.4.90.0030.05716.29
5.4.80.0070.05016.57
5.4.70.0130.04716.61
5.4.60.0030.05316.55
5.4.50.0000.05716.29
5.4.40.0070.05316.41
5.4.30.0130.04316.45
5.4.20.0030.05716.64
5.4.10.0130.04316.51
5.4.00.0170.04016.04
5.3.290.0030.05714.85
5.3.280.0100.05314.73
5.3.270.0130.05014.79
5.3.260.0030.05714.74
5.3.250.0070.05314.59
5.3.240.0170.04314.63
5.3.230.0230.03714.65
5.3.220.0130.04714.52
5.3.210.0070.05714.50
5.3.200.0100.05014.55
5.3.190.0070.05314.64
5.3.180.0130.04714.55
5.3.170.0100.05014.70
5.3.160.0100.05014.64
5.3.150.0000.06014.59
5.3.140.0170.05014.70
5.3.130.0030.05714.57
5.3.120.0200.06314.68
5.3.110.0070.05714.51
5.3.100.0100.05013.98
5.3.90.0100.05014.10
5.3.80.0170.04314.00
5.3.70.0100.05014.04
5.3.60.0130.04714.10
5.3.50.0130.04313.90
5.3.40.0100.04713.83
5.3.30.0130.06313.94
5.3.20.0070.05013.70
5.3.10.0170.04013.61
5.3.00.0070.05013.57
5.2.170.0100.03711.16
5.2.160.0070.04011.34
5.2.150.0030.04311.13
5.2.140.0100.03711.32
5.2.130.0130.04711.28
5.2.120.0100.03711.28
5.2.110.0170.04011.04
5.2.100.0130.03311.14
5.2.90.0130.03311.20
5.2.80.0100.04010.98
5.2.70.0070.04011.23
5.2.60.0070.04311.04
5.2.50.0100.03711.03
5.2.40.0070.05011.08
5.2.30.0100.03710.98
5.2.20.0200.02710.93
5.2.10.0100.03710.79
5.2.00.0170.03710.66
5.1.60.0030.03710.11
5.1.50.0130.0409.94
5.1.40.0030.0379.92
5.1.30.0000.04010.28
5.1.20.0030.03710.33
5.1.10.0030.03710.15
5.1.00.0030.03710.25
5.0.50.0070.0278.58
5.0.40.0000.0338.36
5.0.30.0070.0408.40
5.0.20.0070.0278.37
5.0.10.0030.0338.21
5.0.00.0070.0408.32
4.4.90.0030.0205.84
4.4.80.0000.0275.86
4.4.70.0100.0175.74
4.4.60.0000.0235.84
4.4.50.0000.0235.90
4.4.40.0070.0305.78
4.4.30.0000.0275.79
4.4.20.0030.0205.80
4.4.10.0030.0205.99
4.4.00.0030.0335.89
4.3.110.0000.0235.70
4.3.100.0030.0205.84
4.3.90.0030.0235.80
4.3.80.0030.0335.65
4.3.70.0030.0275.66
4.3.60.0000.0235.65
4.3.50.0000.0235.68
4.3.40.0030.0335.72
4.3.30.0000.0234.47
4.3.20.0070.0174.43
4.3.10.0030.0204.36
4.3.00.0030.0237.01

preferences:
24.54 ms | 403 KiB | 5 Q