Created
June 25, 2012 15:54
-
-
Save vortec/2989401 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 random | |
import sys | |
import time | |
runs = 5 | |
items = 100000 | |
str_length = 12 | |
if sys.version_info.major == 2: | |
def genBytestrings(): | |
""" """ | |
d = {} | |
print('BYTESTRING') | |
start = time.time() | |
for i in range(0, items): | |
random.seed(0) | |
byte_str = ''.join([chr(random.randint(32, 126)) for k in range(0, str_length)]) | |
d[byte_str] = byte_str | |
end = time.time() | |
print('generation time: {}'.format(end-start)) | |
return d | |
def genUnicodes(): | |
""" """ | |
d = {} | |
print('UNICODE') | |
start = time.time() | |
for i in range(0, items): | |
random.seed(0) | |
byte_str = u''.join([unichr(random.randint(32, 126)) for k in range(0, str_length)]) | |
d[byte_str] = byte_str | |
end = time.time() | |
print('generation time: {}'.format(end-start)) | |
return d | |
def compare(d): | |
""" """ | |
start = time.time() | |
for i in range(0, runs): | |
last_key = False | |
for key in d: | |
if last_key and d[key] == d[last_key]: | |
print('you just won the lottery') | |
last_key = key | |
end = time.time() | |
print('comparison time: {}\n'.format(end-start)) | |
print('runs: {0}, items: {1}, str_length: {2}\n'.format(runs, items, str_length)) | |
for i in range(0, runs): | |
d = genBytestrings() | |
compare(d) | |
d = genUnicodes() | |
compare(d) | |
print('-----\n') |
This file contains hidden or 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
$ python2 benchmark.py | |
runs: 5, items: 100000, str_length: 12 | |
BYTESTRING | |
generation time: 4.48273801804 | |
comparison time: 1.21593475342e-05 | |
UNICODE | |
generation time: 4.6273920536 | |
comparison time: 1.50203704834e-05 | |
----- | |
BYTESTRING | |
generation time: 4.49021196365 | |
comparison time: 1.4066696167e-05 | |
UNICODE | |
generation time: 4.63130092621 | |
comparison time: 1.19209289551e-05 | |
----- | |
BYTESTRING | |
generation time: 4.48817205429 | |
comparison time: 1.47819519043e-05 | |
UNICODE | |
generation time: 4.62876796722 | |
comparison time: 1.19209289551e-05 | |
----- | |
BYTESTRING | |
generation time: 4.4868350029 | |
comparison time: 1.28746032715e-05 | |
UNICODE | |
generation time: 4.6292090416 | |
comparison time: 1.12056732178e-05 | |
----- | |
BYTESTRING | |
generation time: 4.48903894424 | |
comparison time: 1.38282775879e-05 | |
UNICODE | |
generation time: 4.67465996742 | |
comparison time: 1.31130218506e-05 | |
----- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Without random.seed(0):
$ python2 benchmark.py
runs: 5, items: 100000, str_length: 12
BYTESTRING
generation time: 3.1144721508
comparison time: 0.157985925674
UNICODE
generation time: 3.45516300201
comparison time: 0.233561992645
BYTESTRING
generation time: 3.17846012115
comparison time: 0.15659403801
UNICODE
generation time: 3.24055600166
comparison time: 0.220932006836
BYTESTRING
generation time: 3.08818984032
comparison time: 0.165657997131
UNICODE
generation time: 3.23957490921
comparison time: 0.223265886307
BYTESTRING
generation time: 3.1283390522
comparison time: 0.159765005112
UNICODE
generation time: 3.21039605141
comparison time: 0.226836919785
BYTESTRING
generation time: 3.09065699577
comparison time: 0.162682056427
UNICODE
generation time: 3.22223496437
comparison time: 0.226697921753