Java UUID generators compared

Posted 2007-04-25 in Java by Johann.

UUIDs (or GUIDs) are unique identifiers that are frequently used in programming. In this entry, I will compare three generators of UUIDs written in the Java programming language.

Possible uses for UUIDs

UUIDs have a variety of uses, including

  • Temporary file names.
  • Unique identifiers for website visitors.
  • Transaction IDs.
  • Primary keys, replacing database sequences.

Generally, whenever unique values across different machines are needed, UUIDs are a good choice.

The contestants

The following packages were compared:

  • UUID – my own implementation.
  • Java UUID Generator.
  • java.util.UUID, available since JDK 5.

I did not compare the following packages:

The features

I compared the following features:

  • Ease of use: How many lines of code does it take to get a UUID?
  • Versions: The UUID versions supported (1–5).
  • Speed: The performance of generating UUIDs.
  • EJB: Whether the generated UUIDs can be used as primary keys in enterprise java beans.
  • CORBA: Whether the generated UUIDs can be used in CORBA.
  • Parse: Whether parsing UUIDs is supported. Usually, this is done from java.lang.Strings, although JUG also supports parsing from byte arrays.

The results

Name

Ease of use

Versions

Speed

EJB

CORBA

Parse

++: very good, +: good, o: acceptable, -: bad, --: very bad

UUID

++

1

++

Yes

Yes

String, CharSequence

JUG

o

1, 3–5

+/-/--

No

No

String, byte[]

JDK 5

++

3, 4

-/--

No

No

String

The benchmark

The performance of all generators was compared. The test computer was an Intel Pentium M 1.73 GHz computer with 1.5 GB of RAM. Load on the computer was low during the tests.

The following Java Virtual Machines were used:

  • Sun JDK 1.5.0_05
  • IBM 1.3.1
  • Sun 1.4.2_09
  • BEA JRockit 1.4.2_08
  • BEA JRockit 1.5.0_08
  • Sun JDK 6

All Java Virtual Machines were started with -Xms256M -Xmx256M to force a fixed VM size.

The data

The following table contains the timings in milliseconds for the creation of one million UUIDs with the various implementations.

Java UUID generators compared

VM

UUID

JUG, time

JUG, name

JUG, random

JDK, random

JDK, name

Sun 1.5.0_05

1315

3424

25518

25946

28565

35253

IBM 1.3.1

2359

6796

81134

46584

Sun 1.4.2_09

2800

7053

91569

64956

JRockit 1.4.2_08

2556

5859

63890

47500

JRockit 1.5.0_08

1431

3756

25653

35093

35797

32253

Sun JDK 6

2303

5703

35968

41100

41553

48859

My own implementation is the fastest by a factor of at least two. With a modern computer, it is possible to generate more than 500.000 UUIDs per second.

The good

All of the implementations I tested generated unique UUIDs, even when used multi-threaded.

The bad

Some of the implementations are slower than others. It might not be possible to use some implementations in environments like EJB containers.

The MAC address issue

The computer’s MAC address must be encoded into a version 1 UUID. Because there is no official way in Java to do this, JUG comes with a native library (compiled for Windows x86, FreeBSD, Linux x86, MacOSX PPC, SPARC). My own version does this without native code (tested on Windows, Linux, MacOSX, BSDs, Solaris, HP-UX) and falls back to random data if the MAC address cannot be read.

Security aspects must be considered when version 1 UUIDs that contain the MAC address are made public.

The summary

If you need speed and/or support for EJBs or CORBA, UUID is the best way. If security is of ultimate importance to you, it cannot be used. In that case, JUG in the time-based mode is a good choice.

2 comments

#1 2010-03-04 by CarlosOrtiz

Can you please send me the info Jethro and unmesh asked for?
"how to prevent concureny program on multi-Jvm on same machine from generating same uuid?"

I also would like to know the answer of:

Can (userid + transaction type + timestamp) a reliable approach to generate unique id for transaction in web applications where user has to log in before performing any transaction and no two users can have same ids?

#2 2010-03-05 by Johann

Carlos,

in addition to the MAC address, there's also 16 bits of randomness in each UUID.

Concerning your second question, I wouldn't use the transaction type, no matter what it is, because I assume a very uneven distribution. You might be happy with something like (userid + shared AtomicLong#incrementAndGet()).

But then, create a multithreaded test for your implementation, add all IDs to a List and see if you get duplicates.

Subscribe

RSS 2.0, Atom or subscribe by Email.

Top Posts

  1. DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
  2. 6 fast jQuery Tips: More basic Snippets
  3. xslt.js version 3.2 released
  4. xslt.js version 3.0 released XML XSLT now with jQuery plugin
  5. Forum Scanners - prevent forum abuse
  6. Automate JavaScript compression with YUI Compressor and /packer/

Navigation