3v4l.org

run code in 300+ PHP versions simultaneously
<?php class PseudoRandomSequence { const STATE_HASH = "sha512"; // 1/2 STATE_HASH block size, const STATE_SIZE = 32; private $state; public function __construct($state) { $this->state = $state; $this->mixState(); } public function int($min = 0, $max = PHP_INT_MAX) { $range = $max - $min; if ($range <= 0 || is_float($range)) { throw new Exception("Invalid min/max provided"); } $bits = 0; $tmp = $range; while ($tmp >>= 1) { $bits++; } $bytes = (int) max(ceil($bits / 8), 1); $mask = pow(2, $bits) - 1; do { $rand = $this->bytes($bytes); $result = hexdec(bin2hex($rand)) & $mask; } while ($result > $range); return $result + $min; } public function bytes($n) { $ret = ''; do { $ret .= $this->mixState(); } while (strlen($ret) < $n); return substr($ret, 0, $n); } private function mixState() { $hash = hash(self::STATE_HASH, $this->state, true); $this->state = $hash; return substr($hash, self::STATE_SIZE); } } $prng = new PseudoRandomSequence("test"); var_dump($prng->int(0, 10)); var_dump($prng->int(0, 10)); var_dump($prng->int(0, 10)); var_dump($prng->int(0, 10)); $prng = new PseudoRandomSequence("other"); var_dump($prng->int(0, 100)); var_dump($prng->int(0, 100)); var_dump($prng->int(0, 100)); var_dump($prng->int(0, 100));

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.70.0090.01218.56
8.3.60.0090.00618.63
8.3.50.0130.00522.02
8.3.40.0150.00618.79
8.3.30.0030.01219.32
8.3.20.0180.00020.43
8.3.10.0070.01021.89
8.3.00.0060.00319.13
8.2.190.0040.01116.63
8.2.180.0110.01118.42
8.2.170.0120.00322.96
8.2.160.0070.00722.25
8.2.150.0030.01424.18
8.2.140.0140.00324.66
8.2.130.0110.00726.16
8.2.120.0050.00319.87
8.2.110.0100.00019.43
8.2.100.0040.00817.88
8.2.90.0080.00019.30
8.2.80.0060.00317.97
8.2.70.0040.00417.63
8.2.60.0060.00317.93
8.2.50.0050.00518.07
8.2.40.0050.00319.97
8.2.30.0040.00418.20
8.2.20.0030.00517.85
8.2.10.0080.00018.21
8.2.00.0000.00817.89
8.1.280.0140.00325.92
8.1.270.0130.00720.77
8.1.260.0000.00726.35
8.1.250.0000.00828.09
8.1.240.0100.00024.02
8.1.230.0090.00317.42
8.1.220.0070.00417.74
8.1.210.0000.00818.77
8.1.200.0030.00617.47
8.1.190.0000.00817.47
8.1.180.0050.00318.10
8.1.170.0000.00820.43
8.1.160.0040.00422.13
8.1.150.0040.00419.07
8.1.140.0040.00417.63
8.1.130.0000.00717.86
8.1.120.0030.00517.57
8.1.110.0040.00417.48
8.1.100.0030.00517.53
8.1.90.0000.00717.63
8.1.80.0000.00817.59
8.1.70.0070.00017.59
8.1.60.0040.00417.74
8.1.50.0080.00017.66
8.1.40.0040.00417.52
8.1.30.0030.00617.84
8.1.20.0040.00417.72
8.1.10.0000.00817.70
8.1.00.0000.00817.48
8.0.300.0000.00718.77
8.0.290.0000.00816.88
8.0.280.0000.00718.61
8.0.270.0050.00217.38
8.0.260.0000.00817.29
8.0.250.0070.00017.02
8.0.240.0000.00717.06
8.0.230.0030.00317.06
8.0.220.0040.00416.98
8.0.210.0070.00017.03
8.0.200.0060.00317.05
8.0.190.0070.00016.99
8.0.180.0000.00817.08
8.0.170.0000.00817.05
8.0.160.0030.00617.09
8.0.150.0030.00516.98
8.0.140.0000.00716.99
8.0.130.0030.00413.40
8.0.120.0040.00416.95
8.0.110.0000.00716.93
8.0.100.0040.00417.07
8.0.90.0060.00316.81
8.0.80.0070.01116.95
8.0.70.0040.00416.89
8.0.60.0040.00416.95
8.0.50.0000.00716.96
8.0.30.0100.01217.25
8.0.20.0150.00717.46
8.0.10.0000.00817.09
8.0.00.0080.01016.75
7.4.330.0030.00315.03
7.4.320.0050.00316.67
7.4.300.0000.00616.62
7.4.290.0030.00316.71
7.4.280.0070.00016.61
7.4.270.0000.00716.66
7.4.260.0070.00016.70
7.4.250.0030.00316.69
7.4.240.0030.00416.60
7.4.230.0000.00716.46
7.4.220.0070.02116.49
7.4.210.0070.01316.66
7.4.200.0050.00316.61
7.4.160.0080.00816.64
7.4.150.0140.00417.40
7.4.140.0130.00817.86
7.4.130.0080.01116.68
7.4.120.0070.01016.69
7.4.110.0120.00616.70
7.4.100.0040.01316.64
7.4.90.0170.00016.53
7.4.80.0130.01419.39
7.4.70.0150.00316.56
7.4.60.0160.00716.64
7.4.50.0040.00416.57
7.4.40.0090.00916.70
7.4.30.0090.01216.83
7.4.00.0040.01114.88
7.3.330.0030.00313.46
7.3.320.0100.00513.29
7.3.310.0070.00016.52
7.3.300.0000.00716.41
7.3.290.0050.01316.42
7.3.280.0130.00616.47
7.3.270.0130.00617.40
7.3.260.0070.01016.48
7.3.250.0100.00816.55
7.3.240.0070.01316.53
7.3.230.0070.01116.75
7.3.210.0180.00016.68
7.3.200.0070.01019.39
7.3.190.0130.00716.46
7.3.180.0070.01016.54
7.3.170.0140.00616.57
7.3.160.0130.01016.54
7.3.10.0090.00616.68
7.3.00.0060.00616.46
7.2.330.0120.00616.87
7.2.320.0120.00416.86
7.2.310.0030.01516.91
7.2.300.0160.00616.85
7.2.290.0130.01216.70
7.2.130.0080.00416.67
7.2.120.0030.00716.71
7.2.110.0040.00816.86
7.2.100.0040.00816.92
7.2.90.0000.01117.04
7.2.80.0030.00616.88
7.2.70.0110.00416.75
7.2.60.0060.00816.88
7.2.50.0060.00616.80
7.2.40.0070.00716.68
7.2.30.0070.00716.85
7.2.20.0080.00317.04
7.2.10.0030.00916.88
7.2.00.0030.01018.20
7.1.250.0000.00915.85
7.1.100.0000.01518.04
7.1.70.0000.00816.93
7.1.60.0060.01919.50
7.1.50.0140.01017.05
7.1.00.0030.07722.34
7.0.200.0070.00415.15
7.0.140.0030.07321.95
7.0.60.0030.04019.97
7.0.50.0170.07017.77
7.0.40.0100.05020.25
7.0.30.0300.05320.09
7.0.20.0370.04020.16
7.0.10.0230.04320.11
7.0.00.0130.08020.14
5.6.280.0000.07721.04
5.6.210.0000.04320.72
5.6.200.0000.04318.19
5.6.190.0000.08020.33
5.6.180.0270.08720.39
5.6.170.0400.04020.45
5.6.160.0030.04020.52
5.6.150.0070.07718.16
5.6.140.0070.08318.15
5.6.130.0070.06718.24
5.6.120.0030.04020.91
5.6.110.0100.06021.00
5.6.100.0030.04720.98
5.6.90.0100.06721.13
5.6.80.0130.07320.42
5.6.70.0230.07320.39
5.5.350.0170.08020.39
5.5.340.0000.04317.96
5.5.330.0100.08020.27
5.5.320.0170.04720.19
5.5.310.0330.04720.25
5.5.300.0100.07318.08
5.5.290.0030.08017.98
5.5.280.0070.08321.00
5.5.270.0130.07720.88
5.5.260.0100.04020.74
5.5.250.0130.08020.56
5.5.240.0000.04320.27
5.4.450.0970.07019.53
5.4.440.0470.06319.33
5.4.430.0670.05719.15
5.4.420.0130.05019.28
5.4.410.0730.06019.04
5.4.400.1130.04718.82
5.4.390.1000.05718.63
5.4.380.1030.04718.75
5.4.370.0930.04718.61
5.4.360.0870.05018.71
5.4.350.1200.05318.74
5.4.340.0930.04718.74
5.4.320.0900.05318.76
5.4.310.1100.04318.52
5.4.300.0830.05718.63
5.4.290.0900.05318.73
5.4.280.0930.05318.59
5.4.270.0870.05318.59
5.4.260.1070.06018.57
5.4.250.1100.05018.46
5.4.240.1130.05318.75
5.4.230.1200.04718.52
5.4.220.1100.05318.72
5.4.210.0930.05018.40
5.4.200.0900.05316.57
5.4.190.1000.04018.56
5.4.180.1330.04018.56
5.4.170.1030.05718.56
5.4.160.1170.04018.61
5.4.150.1200.05018.71
5.4.140.1200.05716.27
5.4.130.1130.04316.25
5.4.120.1000.05316.30
5.4.110.1100.05316.23
5.4.100.1000.05016.23
5.4.90.1000.05316.31
5.4.80.1070.04716.32
5.4.70.1070.05016.28
5.4.60.1070.04716.28
5.4.50.1070.04316.42
5.4.40.1030.05316.42
5.4.30.1100.05016.21
5.4.20.1130.04716.26
5.4.10.1100.04316.26
5.4.00.1130.04315.79
5.3.290.1070.05314.78
5.3.280.1070.05014.63
5.3.270.1130.04314.72
5.3.260.1230.05014.83
5.3.250.1000.05314.62
5.3.240.1070.05314.64
5.3.230.1200.05714.61
5.3.220.1270.03314.61
5.3.210.0870.05714.57
5.3.200.1000.05714.61
5.3.190.1170.05714.58
5.3.180.0970.06014.76
5.3.170.1100.04314.57
5.3.160.0900.05014.64
5.3.150.1130.06314.74
5.3.140.1130.04714.73
5.3.130.1100.05014.75
5.3.120.1230.05014.78
5.3.110.1170.04714.61
5.3.100.1070.05314.23
5.3.90.0930.06314.07
5.3.80.1100.04314.14
5.3.70.0970.05714.20
5.3.60.1030.05314.06
5.3.50.1000.05313.95
5.3.40.1000.06014.19
5.3.30.1100.04714.04
5.3.20.1030.05013.82
5.3.10.1100.04713.78
5.3.00.1170.04713.80
5.2.170.0870.04311.19
5.2.160.0900.03711.21
5.2.150.0800.04011.29
5.2.140.0900.03711.36
5.2.130.0800.04311.25
5.2.120.0670.04311.32
5.2.110.0700.04011.32
5.2.100.0730.04011.32
5.2.90.0670.04311.32
5.2.80.0730.03711.19
5.2.70.0730.04711.19
5.2.60.0800.03711.17
5.2.50.0900.03711.16
5.2.40.0770.03311.04
5.2.30.0630.04711.09
5.2.20.0630.04011.04
5.2.10.0730.03710.98
5.2.00.0800.03010.71
5.1.60.0830.04010.20
5.1.50.0630.03710.05
5.1.40.0730.03710.06
5.1.30.0800.03010.38
5.1.20.0700.03710.48
5.1.10.0670.04310.11
5.1.00.0700.03310.09
5.0.50.0400.0308.78
5.0.40.0370.0238.53
5.0.30.0370.0438.40
5.0.20.0300.0308.27
5.0.10.0370.0378.41
5.0.00.0400.0408.22
4.4.90.0370.0205.92
4.4.80.0370.0205.90
4.4.70.0330.0205.88
4.4.60.0330.0205.88
4.4.50.0300.0235.94
4.4.40.0370.0435.91
4.4.30.0400.0205.93
4.4.20.0330.0236.05
4.4.10.0370.0206.03
4.4.00.0330.0335.95
4.3.110.0330.0235.94
4.3.100.0300.0335.92
4.3.90.0330.0205.84
4.3.80.0330.0335.93
4.3.70.0400.0135.82
4.3.60.0400.0205.77
4.3.50.0370.0235.84
4.3.40.0330.0335.86
4.3.30.0130.0235.02
4.3.20.0130.0235.02
4.3.10.0200.0205.02
4.3.00.0170.0237.30

preferences:
35.95 ms | 401 KiB | 5 Q