3v4l.org

run code in 300+ PHP versions simultaneously
<?php class UUID { public static function v3($namespace, $name) { if(!self::is_valid($namespace)) return false; // Get hexadecimal components of namespace $nhex = str_replace(array('-','{','}'), '', $namespace); // Binary Value $nstr = ''; // Convert Namespace UUID to bits for($i = 0; $i < strlen($nhex); $i+=2) { $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1])); } // Calculate hash value $hash = md5($nstr . $name); return sprintf('%08s-%04s-%04x-%04x-%12s', // 32 bits for "time_low" substr($hash, 0, 8), // 16 bits for "time_mid" substr($hash, 8, 4), // 16 bits for "time_hi_and_version", // four most significant bits holds version number 3 (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, // 48 bits for "node" substr($hash, 20, 12) ); } public static function v4() { return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', // 32 bits for "time_low" mt_rand(0, 0xffff), mt_rand(0, 0xffff), // 16 bits for "time_mid" mt_rand(0, 0xffff), // 16 bits for "time_hi_and_version", // four most significant bits holds version number 4 mt_rand(0, 0x0fff) | 0x4000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 mt_rand(0, 0x3fff) | 0x8000, // 48 bits for "node" mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) ); } public static function v5($namespace, $name) { if(!self::is_valid($namespace)) return false; // Get hexadecimal components of namespace $nhex = str_replace(array('-','{','}'), '', $namespace); // Binary Value $nstr = ''; // Convert Namespace UUID to bits for($i = 0; $i < strlen($nhex); $i+=2) { $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1])); } // Calculate hash value $hash = sha1($nstr . $name); return sprintf('%08s-%04s-%04x-%04x-%12s', // 32 bits for "time_low" substr($hash, 0, 8), // 16 bits for "time_mid" substr($hash, 8, 4), // 16 bits for "time_hi_and_version", // four most significant bits holds version number 5 (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, // 48 bits for "node" substr($hash, 20, 12) ); } public static function is_valid($uuid) { return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1; } } // Usage // Named-based UUID. print UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString'); // Pseudo-random UUID ?>

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.3.10.0160.00616.64
7.3.00.0200.00316.69
7.2.130.0140.00916.95
7.2.120.0130.00316.55
7.2.110.0130.00616.66
7.2.100.0160.01417.10
7.2.90.0110.00616.99
7.2.80.0120.01216.74
7.2.70.0170.01316.81
7.2.60.0080.01116.66
7.2.50.0120.00716.98
7.2.40.0150.00916.54
7.2.30.0190.00316.71
7.2.20.0220.00716.67
7.2.10.0100.01616.67
7.2.00.0120.01217.10
7.1.250.0090.01415.80
7.1.70.0030.01017.41
7.1.60.0170.00719.33
7.1.50.0170.02035.13
7.1.00.0030.06722.44
7.0.200.0320.00614.90
7.0.140.0070.07021.97
7.0.60.0200.07721.75
7.0.50.0070.06717.91
7.0.40.0070.08720.06
7.0.30.0500.06020.18
7.0.20.0130.06020.22
7.0.10.0070.08720.27
7.0.00.0070.08720.13
5.6.210.0070.04020.52
5.6.200.0170.08018.18
5.6.190.0070.04720.63
5.6.180.0370.07020.41
5.6.170.0100.04720.50
5.6.160.0230.07020.50
5.6.150.0030.07318.13
5.6.140.0100.06018.18
5.6.130.0100.07718.14
5.6.120.0100.06721.01
5.6.110.0070.08321.01
5.6.100.0000.04721.03
5.6.90.0070.06321.09
5.6.80.0070.07720.39
5.5.350.4230.04320.45
5.5.340.0030.04017.99
5.5.330.0100.06720.40
5.5.320.0100.06020.42
5.5.310.0370.06720.35
5.5.300.0100.07718.01
5.5.290.0000.08717.99
5.5.280.0000.05320.68
5.5.270.0130.05720.89
5.5.260.0070.04020.79
5.5.250.0100.07720.64
5.5.240.0170.04720.26
5.4.450.0730.05319.30
5.4.440.0670.05719.30
5.4.430.0830.05719.33
5.4.420.0470.05719.38
5.4.410.0870.05019.34
5.4.400.0870.06019.23
5.4.390.0800.06019.29
5.4.380.0330.05018.75
5.4.370.0130.05718.63
5.4.360.0370.05318.49
5.4.350.0230.05318.70
5.4.340.0270.04718.55
5.4.320.0060.03612.52
5.4.310.0050.04512.52
5.4.300.0080.03912.52
5.4.290.0050.03912.52
5.4.280.0080.03612.39
5.4.270.0080.03712.39
5.4.260.0150.03112.39
5.4.250.0050.04012.39
5.4.240.0130.03312.39
5.4.230.0060.04112.38
5.4.220.0090.03812.38
5.4.210.0070.03712.38
5.4.200.0070.03812.38
5.4.190.0020.04212.38
5.4.180.0050.03912.38
5.4.170.0040.04012.39
5.4.160.0090.03712.39
5.4.150.0060.03912.38
5.4.140.0110.03612.06
5.4.130.0070.03712.05
5.4.120.0060.04312.01
5.4.110.0110.03312.00
5.4.100.0120.03612.01
5.4.90.0100.04112.00
5.4.80.0130.04612.00
5.4.70.0110.04712.00
5.4.60.0130.04212.00
5.4.50.0100.05112.00
5.4.40.0080.05311.99
5.4.30.0160.04811.98
5.4.20.0070.05311.98
5.4.10.0120.04511.99
5.4.00.0060.05911.47
5.3.290.0120.03912.80
5.3.280.0100.05812.71
5.3.270.0080.05912.72
5.3.260.0080.06112.72
5.3.250.0130.06212.72
5.3.240.0110.06312.71
5.3.230.0140.05312.71
5.3.220.0110.05712.68
5.3.210.0140.07512.68
5.3.200.0130.05112.68
5.3.190.0190.06512.68
5.3.180.0080.05312.68
5.3.170.0080.05412.67
5.3.160.0060.05712.68
5.3.150.0060.05012.67
5.3.140.0060.04412.66
5.3.130.0100.04212.66
5.3.120.0120.03612.66
5.3.110.0090.03812.66
5.3.100.0080.03912.12
5.3.90.0070.03812.11
5.3.80.0060.04712.09
5.3.70.0070.03912.10
5.3.60.0050.05012.08
5.3.50.0140.05012.03
5.3.40.0080.05212.03
5.3.30.0160.04511.99
5.3.20.0130.05711.77
5.3.10.0120.05011.73
5.3.00.0100.05111.72
5.2.170.0110.0529.22
5.2.160.0220.0679.22
5.2.150.0130.0589.22
5.2.140.0110.0559.22
5.2.130.0080.0439.18
5.2.120.0090.0399.18
5.2.110.0110.0389.19
5.2.100.0130.0389.19
5.2.90.0160.0349.18
5.2.80.0090.0439.18
5.2.70.0050.0459.18
5.2.60.0120.0399.13
5.2.50.0070.0449.11
5.2.40.0110.0369.09
5.2.30.0070.0429.07
5.2.20.0100.0419.05
5.2.10.0070.0468.96
5.2.00.0060.0478.83
5.1.60.0030.0378.11
5.1.50.0050.0318.11
5.1.40.0070.0298.09
5.1.30.0040.0278.44
5.1.20.0060.0348.46
5.1.10.0070.0398.18
5.1.00.0040.0398.18
5.0.50.0060.0366.66
5.0.40.0080.0296.54
5.0.30.0060.0446.34
5.0.20.0080.0286.30
5.0.10.0040.0316.29
5.0.00.0050.0426.27
4.4.90.0040.0214.78
4.4.80.0110.0124.76
4.4.70.0060.0124.75
4.4.60.0030.0154.75
4.4.50.0040.0164.77
4.4.40.0030.0264.71
4.4.30.0040.0154.76
4.4.20.0040.0154.84
4.4.10.0040.0154.85
4.4.00.0040.0244.76
4.3.110.0040.0154.67
4.3.100.0030.0204.66
4.3.90.0040.0184.63
4.3.80.0030.0344.59
4.3.70.0040.0174.63
4.3.60.0030.0164.62
4.3.50.0060.0144.63
4.3.40.0060.0284.54
4.3.30.0020.0223.29
4.3.20.0060.0263.27
4.3.10.0040.0203.23
4.3.00.0130.02010.37

preferences:
34.84 ms | 401 KiB | 5 Q