Created
February 19, 2020 10:59
-
-
Save HelloZeroNet/394e653870d87cb862353b6108e84832 to your computer and use it in GitHub Desktop.
Dict key access speed test
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 msgpack | |
import msgpack.fallback | |
import time | |
import random | |
def test_dict_key_access(keys): | |
s = time.time() | |
data = {} | |
for key in keys: | |
data[key] = True | |
s = time.time() | |
for i in range(100): | |
for key in keys: | |
res = data[key] | |
print("- Access: {:.3f}s".format(time.time() - s)) | |
s = time.time() | |
keys_random = list(keys) | |
random.shuffle(keys_random) | |
for i in range(100): | |
for key in keys_random: | |
res = data[key] | |
print("- Random access: {:.3f}s".format(time.time() - s)) | |
s = time.time() | |
packed = msgpack.packb(data) | |
for i in range(100): | |
msgpack.unpackb(packed, strict_map_key=False) | |
print("- Msgpack unpack: {:.3f}s".format(time.time() - s)) | |
s = time.time() | |
for i in range(10): | |
msgpack.fallback.unpackb(packed, strict_map_key=False) | |
print("- Msgpack unpack (fallback): {:.3f}s".format(time.time() - s)) | |
s = time.time() | |
print("Dict with same lower bits:") | |
test_dict_key_access(range(42, 100000000, 1024)) | |
print("Dict with different lower bits:") | |
test_dict_key_access(range(0, int(100000000 / 1024))) | |
print("Dict with string keys:") | |
test_dict_key_access([str(key) for key in range(0, int(100000000 / 1024))]) |
Author
HelloZeroNet
commented
Feb 19, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment