Search code examples
javauuid

Performance of Random UUID generation with Java 7 or Java 6


I have a web based Java application that generates random UUIDs for session information. One of our testers is claiming up to 350ms to generate UUIDs based upon his own profiling, but I have not yet been able to replicate his results. He points to this article http://www.cowtowncoder.com/blog/archives/2010/10/entry_429.html to help back up his results. I wanted to see if anyone else has ran into this limitation with Java's built-in UUID generation capability in either Java 6 or Java 7 applications.


Solution

  • I tested it

        for (;;) {
            long t0 = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) {
                UUID.randomUUID();
            }
            System.out.println(System.currentTimeMillis() - t0);
        }
    

    on my PC it is ~1100 ms, which is pretty slow. UUID.randomUUID() uses SecureRandom internally, to make it faster we can use regular java.util.Random

        Random r = new Random();
        for (;;) {
                ..
                new UUID(r.nextLong(), r.nextLong());
    

    it's ~80 ms