Skip to content

Instantly share code, notes, and snippets.

@dln
Created May 26, 2010 08:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dln/414214 to your computer and use it in GitHub Desktop.
Save dln/414214 to your computer and use it in GitHub Desktop.
Quick benchmark of time-based UUID generation in python, clojure/java and C.

TimeUUIDs in Python are slow. Clojure (Java) is fast.

Python

85 >>> def test():
    for _ in xrange(1000000):
        uuid.uuid1()
  ...: 

88 >>> %timeit test()                                                                                                                                      
1 loops, best of 3: 40.3 s per loop

=> 24 813 per second

See also:

Python (using python-libuuid extension)

3 >>> def test():
    for _ in xrange(1000000):
  ..:         libuuid.uuid1()
  ..:
4 >>> %timeit test()
1 loops, best of 3: 12.4 s per loop

=> 80 645 per second

5 >>> def test():
    for _ in xrange(1000000):
  ..:         libuuid.uuid1_bytes()
  ..:
6 >>> %timeit test()
1 loops, best of 3: 8.07 s per loop

=> 123 915 per second

See also:

Clojure / Java

user >>> (import [com.eaio.uuid])
nil

user >>> (time (dorun (repeatedly 1000000 #(com.eaio.uuid.UUID.))))
"Elapsed time: 561.999 msecs"
nil

=> 1 779 362 per second

user >>> (import [org.safehaus.uuid])
nil

user >>> (let [gen (org.safehaus.uuid.UUIDGenerator/getInstance)]
              (time (dorun (repeatedly 1000000 #(.generateTimeBasedUUID gen)))))
"Elapsed time: 641.732 msecs"

=> 1 558 283 per second

See also:

C (libuuid)

$  cat << EOF | gcc -O2 -xc -luuid - && time ./a.out
> #include <uuid/uuid.h>
> 
> int main(int argc, char **argv)
> {
>   int i;
>   uuid_t uuid;
>   for (i=0; i<1000000; i++) {
>     uuid_generate_time(uuid);
>   }
> }
> EOF

real    0m7.093s
user    0m0.792s
sys     0m6.161s

=> 140 984 per second

This seems really slow. Anyone know why?

See also:

@dln
Copy link
Author

dln commented Sep 19, 2010

@pyeprog
Copy link

pyeprog commented Feb 7, 2020

why java implements are 10 times faster than C? It looks weired to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment