# >>> uuid.uuid4()
# UUID('84e952e3-3ce0-43aa-a67d-ace7e55f51ac')
# uuid.uuid4().hex
# '84e952e33ce043aaa67dace7e55f51ac
# assert 32 == len( uuid.uuid4().hex ) # useful for AES 256 bit key (32 bytes * 8 )
# 2013-05-21 johnpfeiffer - take all benchmarks with a dose of salt: order of operations, caching, os variances, interpreter, etc.
import time
import uuid
def slow_get_uuid():
return str( uuid.uuid4() ) + "-" + slow_get_time_suffix()
def slow_get_time_suffix():
""" a very long way to round down to epoch seconds """
s = str( time.time() )
i = s.find( '.' )
if i > 0:
s = s[:i]
return hex( int(s) )[2:]
def get_uuid_fast():
return '-'.join( [ str( uuid.uuid4() ) , str( int( time.time() ) ) ] )
def get_uuid():
return '-'.join( [ str( uuid.uuid4() ) , hex( int( time.time() ) )[2:] ] )
if __name__ == '__main__':
MAX = 1000
start = time.time()
print uuid.uuid4()
print "uuid4 one done: {} ".format( time.time() - start ) # uuid4 one done: 0.00600004196167
for i in xrange( MAX ):
uuid.uuid4()
print "uuid4 {} times: {} \n".format( MAX, time.time() - start ) # uuid4 1000 times: 0.0350000858307
time.sleep( 2 ) # prevent caching
start = time.time()
print uuid.uuid1(), "may be insecure as it uses the node MAC and current time which creates repetitive guessable (non unique) data"
print "uuid1 one done: {} ".format( time.time() - start ) # uuid1 one done: 0.0090000629425
for i in xrange( MAX ):
uuid.uuid1()
print "uuid1 {} times: {} \n".format( MAX, time.time() - start ) # uuid1 1000 times: 0.0280001163483
time.sleep( 2 ) # prevent caching
start = time.time()
print slow_get_uuid()
print "slow one done: {} ".format( time.time() - start ) # slow one done: 0.007000207901
for i in xrange( MAX ):
slow_get_uuid()
print "slow {} times: {} \n".format( MAX, time.time() - start ) # slow 1000 times: 0.0460000038147
time.sleep( 2 ) # prevent caching
start = time.time()
print get_uuid()
print "custom hex one done: {}".format( time.time() - start ) # custom hex one done: 0.00600004196167
for i in xrange( MAX ):
get_uuid()
print "custom hex {} times:{} \n".format( MAX, time.time() - start ) # custom hex 1000 times:0.0389997959137
time.sleep( 2 ) # prevent caching
start = time.time()
print get_uuid_fast()
print "custom one done: {}".format( time.time() - start ) #custom one done: 0.00600004196167
for i in xrange( MAX ):
get_uuid_fast()
print "custom {} times: {} \n".format( MAX, time.time() - start ) # custom 1000 times: 0.0370001792908
time.sleep( 2 ) # prevent caching
print "example epoch seconds . milliseconds", repr( time.time() ), '\n' # print time.time() rounds off the last millisecond!