Last active
August 29, 2015 14:04
-
-
Save kevinlondon/d3bb32d5a784f78731fa to your computer and use it in GitHub Desktop.
UUID Byte Benchmark Test for Cpython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
import time | |
import sys | |
from binascii import unhexlify | |
N = int(sys.argv[1]) | |
class UpdatedUUID(uuid.UUID): | |
@property | |
def bytes(self): | |
return unhexlify(self.hex) | |
def run_test(): | |
original_times = 0 | |
updated_times = 0 | |
for x in xrange(N): | |
original_uuid = uuid.uuid4() | |
updated_uuid = UpdatedUUID(hex=original_uuid.hex) | |
compare_uuids(original_uuid, updated_uuid) | |
original_times += benchmark(original_uuid) | |
updated_times += benchmark(updated_uuid) | |
print "Original Average: %s" % format_time(original_times) | |
print "Updated Average: %s" % format_time(updated_times) | |
def format_time(runtime): | |
return ('%.3f microseconds' % (runtime * 1000000.0 / N)) | |
def benchmark(uuid_object): | |
t = time.time() | |
uuid_object.bytes | |
return time.time() - t | |
def compare_uuids(original, updated): | |
if original.bytes != updated.bytes: | |
print "Original UUID does not match updated UUID byte representation." | |
sys.exit(1) | |
run_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment