Skip to content

Instantly share code, notes, and snippets.

@HelloZeroNet
Created February 19, 2020 10:59
Show Gist options
  • Save HelloZeroNet/394e653870d87cb862353b6108e84832 to your computer and use it in GitHub Desktop.
Save HelloZeroNet/394e653870d87cb862353b6108e84832 to your computer and use it in GitHub Desktop.
Dict key access speed test
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))])
@HelloZeroNet
Copy link
Author

HelloZeroNet commented Feb 19, 2020

Dict with same lower bits:
- Access: 0.677s
- Random access: 2.204s
- Msgpack unpack: 1.437s
- Msgpack unpack (fallback): 3.703s
Dict with different lower bits:
- Access: 0.488s
- Random access: 2.015s
- Msgpack unpack: 1.169s
- Msgpack unpack (fallback): 3.616s
Dict with string keys:
- Access: 0.415s
- Random access: 1.309s
- Msgpack unpack: 3.185s
- Msgpack unpack (fallback): 3.806s

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