import struct
import numpy as np
import time
s = b'\x23\xff' * (16777216 // 256)
arr = np.fromstring(s, dtype=np.uint16)
vals = {}
for i in range(2**16):
vals[struct.pack('H', i)] = tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i)))
t = time.time()
for i in range(0, len(s), 2):
vals[s[i:i+2]]
print("slicing and byte key: %.3f" % (time.time() - t))
vals = {}
for i in range(2**16):
vals[i] = tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i)))
t = time.time()
for uint in arr:
vals[uint]
print("numpy and int key: %.3f" % (time.time() - t))
vals = []
for i in range(2**16):
vals.append(tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i))))
t = time.time()
for uint in arr:
vals[uint]
print("numpy and list: %.3f" % (time.time() - t))
aW1wb3J0IHN0cnVjdAppbXBvcnQgbnVtcHkgYXMgbnAKaW1wb3J0IHRpbWUKCnMgPSBiJ1x4MjNceGZmJyAqICgxNjc3NzIxNiAvLyAyNTYpCgphcnIgPSBucC5mcm9tc3RyaW5nKHMsIGR0eXBlPW5wLnVpbnQxNikKCnZhbHMgPSB7fQpmb3IgaSBpbiByYW5nZSgyKioxNik6Cgl2YWxzW3N0cnVjdC5wYWNrKCdIJywgaSldID0gdHVwbGUoYicxJyBpZiBlID09ICcxJyBlbHNlIGInMCcgZm9yIGUgaW4gbGlzdCgiezowMTZifSIuZm9ybWF0KGkpKSkKCnQgPSB0aW1lLnRpbWUoKQoKZm9yIGkgaW4gcmFuZ2UoMCwgbGVuKHMpLCAyKToKCXZhbHNbc1tpOmkrMl1dCnByaW50KCJzbGljaW5nIGFuZCBieXRlIGtleTogJS4zZiIgJSAodGltZS50aW1lKCkgLSB0KSkKCnZhbHMgPSB7fQpmb3IgaSBpbiByYW5nZSgyKioxNik6Cgl2YWxzW2ldID0gdHVwbGUoYicxJyBpZiBlID09ICcxJyBlbHNlIGInMCcgZm9yIGUgaW4gbGlzdCgiezowMTZifSIuZm9ybWF0KGkpKSkKCnQgPSB0aW1lLnRpb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