Start optimising time-based UUID.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 30 Aug 2020 12:32:00 +0000 (14:32 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 30 Aug 2020 12:32:00 +0000 (14:32 +0200)
org.argeo.util/src/org/argeo/util/UuidUtils.java

index 788d6b9b883f01d52954ee82788d219d4461c09d..d0537a555c87106e9cc4fc6ee1d307a3920e203a 100644 (file)
@@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 public class UuidUtils {
        /** Nil UUID (00000000-0000-0000-0000-000000000000). */
        public final static UUID NIL_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000");
+       public final static LocalDateTime GREGORIAN_START = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
 
        private final static SecureRandom RANDOM;
        private final static AtomicInteger CLOCK_SEQUENCE;
@@ -77,8 +78,7 @@ public class UuidUtils {
 
        public static UUID timeBasedUUID(LocalDateTime time, BitSet node) {
                // most significant
-               LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
-               Duration duration = Duration.between(start, time);
+               Duration duration = Duration.between(GREGORIAN_START, time);
 
                // Number of 100 ns intervals in one second: 1000000000 / 100 = 10000000
                long timeNanos = duration.getSeconds() * 10000000 + duration.getNano() / 100;
@@ -240,33 +240,29 @@ public class UuidUtils {
        public final static void main(String[] args) {
                UUID uuid;
 
-               uuid= compactToUuid("996b1f5122de4b2f94e49168d32f22d1");
+               uuid = compactToUuid("996b1f5122de4b2f94e49168d32f22d1");
                System.out.println(uuid.toString() + ", isRandom=" + isRandom(uuid));
 
                // warm up
-               UUID.randomUUID();
-               timeBasedRandomUUID();
-               timeBasedUUID();
-               UUID.randomUUID();
-               timeBasedRandomUUID();
-               timeBasedUUID();
-               UUID.randomUUID();
-               timeBasedRandomUUID();
-               timeBasedUUID();
-               
+               for (int i = 0; i < 10; i++) {
+                       UUID.randomUUID();
+                       timeBasedRandomUUID();
+                       timeBasedUUID();
+               }
+
                long begin;
                long duration;
-               
+
                begin = System.nanoTime();
                uuid = UUID.randomUUID();
                duration = System.nanoTime() - begin;
                System.out.println(uuid.toString() + " in " + duration + " ns, isRandom=" + isRandom(uuid));
-               
+
                begin = System.nanoTime();
                uuid = timeBasedUUID();
                duration = System.nanoTime() - begin;
                System.out.println(uuid.toString() + " in " + duration + " ns, isTimeBasedRandom=" + isTimeBasedRandom(uuid));
-               
+
                begin = System.nanoTime();
                uuid = timeBasedRandomUUID();
                duration = System.nanoTime() - begin;