3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TimeUUID extends DateTime { // Interval between Julian and Gregorian calendar in 100nanoseconds const Interval = "122192928000000000"; const Time = 1; const DCE = 2; /** * Returns true if this UUID is parsable * * @param string $uuid * @return boolean */ private static function isUUID($uuid) { if (preg_match('/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/', $uuid)) return true; return false; } /** * Ensure the value is not a signed extracted value * * @param char $value * @return char */ private static function ensureNotSigned8bits($value) { return (256 + $value) % 256; } /** * Create data according to the two given raw values * * @param integer $raw1 * @param integer $raw2 * @return char array[8] */ private static function createData($raw1, $raw2) { $b1 = ($raw1 & 0xFF000000) >> 24; $b2 = ($raw1 & 0x00FF0000) >> 16; $b3 = ($raw1 & 0x0000FF00) >> 8; $b4 = ($raw1 & 0x000000FF); $b5 = ($raw2 & 0xFF000000) >> 24; $b6 = ($raw2 & 0x00FF0000) >> 16; $b7 = ($raw2 & 0x0000FF00) >> 8; $b8 = ($raw2 & 0x000000FF); return array( TimeUUID::ensureNotSigned8bits($b1), // 8bits $b2, // 8bits $b3, // 8bits $b4, // 8bits TimeUUID::ensureNotSigned8bits($b5), // 8bits $b6, // 8bits $b7, // 8bits $b8, // 8bits ); } /** * Extract fields from the given UUID * * @param string $uuid * @return array */ private static function extractFields($uuid) { // remove dashes $uuid = preg_replace("/[^a-f0-9]/is", "", $uuid); $data1 = hexdec(substr($uuid, 0, 8)); $data2 = hexdec(substr($uuid, 8, 4)); $data3 = hexdec(substr($uuid, 12, 4)); $uuid332 = hexdec(substr($uuid, 16, 8)); $uuid432 = hexdec(substr($uuid, 24, 8)); return array( $data1, // 32 bits $data2, // 16 bits $data3, // 16 bits TimeUUID::createData($uuid332, $uuid432) ); } /** * Build the UUID using the raw values * * @param integer $data1 * @param integer $data2 * @param integer $data3 * @param char array[8] $data4 * @return string */ private static function composeFields($data1, $data2, $data3, $data4) { $data1Str = str_pad(dechex($data1), 8, '0', STR_PAD_LEFT); $data2Str = str_pad(dechex($data2), 4, '0', STR_PAD_LEFT); $data3Str = str_pad(dechex($data3), 4, '0', STR_PAD_LEFT); $data4Str0 = str_pad(dechex($data4[0]), 2, '0', STR_PAD_LEFT); $data4Str1 = str_pad(dechex($data4[1]), 2, '0', STR_PAD_LEFT); $result = "$data1Str-$data2Str-$data3Str-$data4Str0$data4Str1-"; foreach (range(2, 7) as $index) $result .= str_pad(dechex($data4[$index]), 2, '0', STR_PAD_LEFT); return $result; } /** * Check if the variant of this UUID 4rd node is a DCE * * @param integer $data4 * @return boolean */ private static function checkVariantDCE($data4) { return ($data4[0] & 0xC0) == (TimeUUID::DCE << 6); } /** * Check if the version of this UUID 3rd node is a Time * * @param integer $data3 * @return boolean */ private static function checkVersionTime($data3) { return ($data3 >> 12) == (TimeUUID::Time); } /** * Parse the given uuid, check if it's a TimeUUID (RFC4144), and returns parsed data * * @param type $uuid * @return array( * Unix Timestamp, * Data, * 100nanoseconds * ) */ private static function parse($uuid) { list($data1, $data2, $data3, $data4) = TimeUUID::extractFields($uuid); if (!TimeUUID::checkVariantDCE($data4)) throw new Exception("Not a valid TimeUUID: DCE not found (Variant)"); if (!TimeUUID::checkVersionTime($data3)) throw new Exception("Not a valid TimeUUID: Time not found (Version)"); // $time = ($data1 | ($data2 << 32) | (($data3 & 0xFFF) << 48)) - TimeUUID::Interval; $time = bcsub( bcadd( bcadd( bcmul( $data2, "4294967296", // 0xFFFFFFFF 0 ), $data1 ), bcmul( $data3 & 0xFFF, "281474976710656", // 0xFFFFFFFFFFFF 0 ) ), TimeUUID::Interval ); return array( // intval($time / 10000000), intval(bcdiv($time, 10000000, 0)), $data4, // intval($time % 10000000), intval(bcmod($time, 10000000)) ); } private $data = array( 0, 0, 0, 0, 0, 0, 0, 0 ); private $ticks = 0; /** * Returns $this data * * @return char array[8] */ public function getData() { return $this->data; } /** * Specify the data this TimeUUID will holds. Check TimeUUIDMax for an example * * @param char array[8] $data */ public function setData($data) { $this->data = $data; } /** * Returns $this 100nanoseconds * * @return integer */ public function getTicks() { return $this->ticks; } /** * Since DateTime doesn't support 100nanoseconds, you can give here the * missing accuracy of DateTime to build a complete TimeUUID * * @param integer $ticks */ public function setTicks($ticks) { $this->ticks = $ticks; } /** * Attempt to construct a DateTime based on the given TimeUUID, fallback on * default DateTime constructor if the given $time is not a TimeUUID * * @param type $time * @param DateTimeZone $timezone (ignored when passing a TimeUUID in $time) */ public function __construct($time, $timezone = null) { if (TimeUUID::isUUID($time)) { // TimeUUID are always UTC, and timezone parameter is ignored // when ctoring with @ syntax list($time, $this->data, $this->ticks) = TimeUUID::parse($time); parent::__construct('@' . $time); } else { parent::__construct($time, $timezone); } } /** * Return $this TimeUUID * * @return string */ public function __toString() { // ((Timesteamp() * 10000000) + Interval) + Ticks $time = bcadd( bcadd( bcmul( $this->getTimestamp(), 10000000 ), TimeUUID::Interval ), $this->getTicks() ); // $time & 32 (0xFFFFFFFF => 2^32) $data1 = bcmod($time, "4294967296"); // $time >> 32 $timeHigh = bcdiv($time, "4294967296", 0); $data2 = $timeHigh & 0xFFFF; $data3 = (($timeHigh >> 16) & 0xFFF) | TimeUUID::Time << 12; $data = $this->data; $data[0] = ($data[0] & 0x3F) | TimeUUID::DCE << 6; return TimeUUID::composeFields($data1, $data2, $data3, $data); } } $timeUuid = new TimeUUID("ffffffff-ffff-1fff-8000-000000000000"); $timeUuid->format(DateTime::ISO8601); $timeUuid = new TimeUUID("00000000-0000-1000-8fff-ffffffffffff"); $timeUuid->format(DateTime::ISO8601);

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.0070.01117.00
8.3.50.0050.01022.07
8.3.40.0090.00919.16
8.3.30.0040.01119.35
8.3.20.0030.00620.34
8.3.10.0030.00623.48
8.3.00.0000.00819.27
8.2.180.0100.00718.66
8.2.170.0090.00922.96
8.2.160.0100.01020.70
8.2.150.0060.00324.18
8.2.140.0030.00524.66
8.2.130.0040.00426.16
8.2.120.0090.00920.97
8.2.110.0040.00722.44
8.2.100.0000.01318.28
8.2.90.0050.00319.17
8.2.80.0040.00418.25
8.2.70.0060.00618.25
8.2.60.0000.00918.05
8.2.50.0000.00818.07
8.2.40.0050.00318.34
8.2.30.0040.00418.42
8.2.20.0050.00318.21
8.2.10.0000.00818.30
8.2.00.0000.00818.25
8.1.280.0080.00825.92
8.1.270.0040.00424.00
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0070.00323.97
8.1.230.0030.00919.09
8.1.220.0060.00317.93
8.1.210.0000.00818.77
8.1.200.0040.00817.73
8.1.190.0000.00917.85
8.1.180.0080.00018.10
8.1.170.0080.00018.96
8.1.160.0040.00422.21
8.1.150.0040.00419.15
8.1.140.0050.00317.93
8.1.130.0040.00417.96
8.1.120.0000.00717.81
8.1.110.0020.00517.79
8.1.100.0000.00817.87
8.1.90.0030.00518.00
8.1.80.0000.00817.83
8.1.70.0070.00017.85
8.1.60.0060.00317.83
8.1.50.0040.00417.80
8.1.40.0070.00417.90
8.1.30.0030.00817.94
8.1.20.0030.00717.95
8.1.10.0030.00617.86
8.1.00.0050.00318.03
8.0.300.0000.00918.77
8.0.290.0050.00317.14
8.0.280.0040.00418.51
8.0.270.0000.00817.36
8.0.260.0030.00317.32
8.0.250.0000.00717.26
8.0.240.0030.00317.43
8.0.230.0040.00417.36
8.0.220.0000.00717.35
8.0.210.0070.00017.36
8.0.200.0040.00417.39
8.0.190.0080.00017.36
8.0.180.0050.00317.36
8.0.170.0090.00017.39
8.0.160.0000.00817.23
8.0.150.0080.00417.32
8.0.140.0070.00317.26
8.0.130.0030.00413.70
8.0.120.0050.00317.34
8.0.110.0030.00517.34
8.0.100.0000.01117.40
8.0.90.0040.00417.16
8.0.80.0060.00917.30
8.0.70.0030.00617.29
8.0.60.0000.00817.36
8.0.50.0030.00617.40
8.0.30.0130.00917.57
8.0.20.0130.00617.54
8.0.10.0080.00017.47
8.0.00.0070.01217.29
7.4.330.0000.00515.22
7.4.320.0060.00016.87
7.4.300.0000.00716.82
7.4.290.0040.00416.95
7.4.280.0000.00817.04
7.4.270.0040.00417.05
7.4.260.0050.00216.95
7.4.250.0030.00517.02
7.4.240.0020.00517.01
7.4.230.0030.00317.13
7.4.220.0120.00616.96
7.4.210.0080.00716.89
7.4.200.0040.00417.03
7.4.190.0000.00817.01
7.4.160.0120.01216.97
7.4.150.0130.00617.40
7.4.140.0080.01217.86
7.4.130.0030.01516.97
7.4.120.0090.01417.01
7.4.110.0090.00916.98
7.4.100.0000.02116.95
7.4.90.0070.01116.89
7.4.80.0060.01819.39
7.4.70.0100.01416.93
7.4.60.0140.00716.88
7.4.50.0060.00316.89
7.4.40.0070.01616.91
7.4.30.0000.01717.04
7.4.00.0100.00915.25
7.3.330.0030.00313.54
7.3.320.0030.00313.66
7.3.310.0080.00016.77
7.3.300.0030.00316.71
7.3.290.0040.01116.76
7.3.280.0070.01216.73
7.3.270.0170.00017.40
7.3.260.0090.01016.94
7.3.250.0120.00617.00
7.3.240.0030.01316.71
7.3.230.0090.00916.86
7.3.210.0110.00717.06
7.3.200.0070.01119.39
7.3.190.0090.00916.86
7.3.180.0100.01317.16
7.3.170.0120.00616.89
7.3.160.0030.01316.94
7.3.120.0070.01015.26
7.3.110.0100.01015.61
7.3.100.0070.01115.33
7.3.90.0110.00415.10
7.3.80.0070.01015.12
7.3.70.0040.00715.41
7.3.60.0170.00015.08
7.3.50.0060.01215.23
7.3.40.0080.00815.34
7.3.30.0070.00715.26
7.3.20.0030.01017.15
7.3.10.0100.00717.05
7.3.00.0060.00916.86
7.2.330.0130.00717.23
7.2.320.0070.01117.15
7.2.310.0100.01017.20
7.2.300.0100.00817.16
7.2.290.0120.00617.23
7.2.250.0110.00715.80
7.2.240.0130.00715.49
7.2.230.0030.01615.67
7.2.220.0070.00415.51
7.2.210.0040.01115.66
7.2.200.0030.01015.59
7.2.190.0070.00715.40
7.2.180.0100.00715.62
7.2.170.0030.01415.66
7.2.60.0040.01117.07
7.2.00.0000.01220.00
7.1.330.0120.00316.17
7.1.320.0040.00415.81
7.1.310.0060.00615.93
7.1.300.0040.01416.07
7.1.290.0060.00316.12
7.1.280.0030.01016.09
7.1.270.0070.00716.13
7.1.260.0100.00715.90
7.1.200.0030.01016.39
7.1.100.0030.00918.50
7.1.70.0030.00617.39
7.1.60.0140.01019.46
7.1.50.0100.01317.13
7.1.00.0000.08022.35
7.0.200.0000.00916.87
7.0.140.0000.07321.95
7.0.100.0100.08020.04
7.0.90.0170.04020.03
7.0.80.0170.07720.02
7.0.70.0130.07320.02
7.0.60.0100.03320.20
7.0.50.0000.05320.41
7.0.40.0170.03320.11
7.0.30.0030.08020.04
7.0.20.0100.03720.17
7.0.10.0070.07020.17
7.0.00.0000.05720.17
5.6.280.0100.06720.84
5.6.250.0170.07020.68
5.6.240.0100.05720.75
5.6.230.0000.08720.54
5.6.220.0030.04020.80
5.6.210.0070.08020.52
5.6.200.0130.06721.14
5.6.190.0000.04721.02
5.6.180.0100.07321.10
5.6.170.0070.07721.10
5.6.160.0070.07320.99
5.6.150.0100.06020.98
5.6.140.0100.06721.19
5.6.130.0100.08321.10
5.6.120.0070.04021.08
5.6.110.0030.08321.12
5.6.100.0100.04721.13
5.6.90.0130.03321.04
5.6.80.0100.06320.37
5.6.70.0070.03720.48
5.6.60.0000.04020.41
5.6.50.0130.07320.52
5.6.40.0100.03320.39
5.6.30.0100.04720.47
5.6.20.0000.07720.58
5.6.10.0030.06020.48
5.6.00.0070.06720.35
5.5.380.0100.07720.41
5.5.370.0100.03320.46
5.5.360.0030.07320.44
5.5.350.0130.07320.42
5.5.340.0100.07720.87
5.5.330.0070.04020.92
5.5.320.0130.07020.98
5.5.310.0100.08320.79
5.5.300.0130.03320.80
5.5.290.0070.05320.99
5.5.280.0100.07320.92
5.5.270.0030.04020.88
5.5.260.0070.04020.98
5.5.250.0130.04320.78
5.5.240.0030.04720.33
5.5.230.0100.07720.08
5.5.220.0100.06720.06
5.5.210.0100.06720.33
5.5.200.0030.07020.18
5.5.190.0100.06320.28
5.5.180.0030.04720.14
5.5.160.0070.06720.25
5.5.150.0100.07320.14
5.5.140.0030.07320.23
5.5.130.0170.07020.31
5.5.120.0070.03720.23
5.5.110.0100.03320.26
5.5.100.0070.04020.14
5.5.90.0100.07020.17
5.5.80.0100.03720.06
5.5.70.0130.07720.16
5.5.60.0100.06020.09
5.5.50.0070.06020.10
5.5.40.0030.08320.11
5.5.30.0030.07020.14
5.5.20.0070.08020.09
5.5.10.0030.07720.12
5.5.00.0070.07320.13
5.4.450.0070.06719.54
5.4.440.0130.06019.38
5.4.430.0030.06319.44
5.4.420.0070.06319.40
5.4.410.0030.06319.08
5.4.400.0100.07719.05
5.4.390.0030.07719.05
5.4.380.0200.06019.03
5.4.370.0100.06719.23
5.4.360.0130.07318.95
5.4.350.0100.07719.23
5.4.340.0030.08019.13
5.4.320.0000.05318.85
5.4.310.0070.05018.88
5.4.300.0070.08019.20
5.4.290.0000.07319.16
5.4.280.0070.04019.16
5.4.270.0070.07018.85
5.4.260.0070.03319.14
5.4.250.0100.07019.15
5.4.240.0030.03718.84
5.4.230.0070.04318.87
5.4.220.0100.05319.14
5.4.210.0070.06018.84
5.4.200.0130.07319.04
5.4.190.0130.05719.21
5.4.180.0000.04019.20
5.4.170.0100.06718.93
5.4.160.0030.07719.11
5.4.150.0070.03318.86
5.4.140.0070.04016.51
5.4.130.0100.02716.56
5.4.120.0030.04016.51
5.4.110.0100.06716.32
5.4.100.0030.03716.39
5.4.90.0130.05016.61
5.4.80.0030.05316.54
5.4.70.0100.03016.46
5.4.60.0000.07316.55
5.4.50.0070.03016.50
5.4.40.0070.05716.38
5.4.30.0030.05016.51
5.4.20.0000.04016.41
5.4.10.0030.06016.37
5.4.00.0000.07315.93
5.3.290.0030.08314.87
5.3.280.0100.06714.77
5.3.270.0130.06314.61
5.3.260.0070.08014.73
5.3.250.0030.04714.65
5.3.240.0070.07014.73
5.3.230.0030.05714.72
5.3.220.0030.03714.66
5.3.210.0070.03314.77
5.3.200.0100.04714.61
5.3.190.0000.08014.69
5.3.180.0070.07014.73
5.3.170.0100.07314.65
5.3.160.0000.06314.69
5.3.150.0070.07714.75
5.3.140.0000.06314.57
5.3.130.0030.07014.61
5.3.120.0000.04014.66
5.3.110.0070.07314.65
5.3.100.0100.03314.22
5.3.90.0130.05714.03
5.3.80.0030.03314.19
5.3.70.0000.07014.20
5.3.60.0030.05313.99
5.3.50.0100.07314.07
5.3.40.0030.08014.15
5.3.30.0070.06314.08
5.3.20.0000.07713.74
5.3.10.0070.06313.69
5.3.00.0070.07313.78

preferences:
41.68 ms | 400 KiB | 5 Q