fork download
  1. import struct
  2. import numpy as np
  3. import time
  4.  
  5. s = b'\x23\xff' * (16777216 // 256)
  6.  
  7. arr = np.fromstring(s, dtype=np.uint16)
  8.  
  9. vals = {}
  10. for i in range(2**16):
  11. vals[struct.pack('H', i)] = tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i)))
  12.  
  13. t = time.time()
  14.  
  15. for i in range(0, len(s), 2):
  16. vals[s[i:i+2]]
  17. print("slicing and byte key: %.3f" % (time.time() - t))
  18.  
  19. vals = {}
  20. for i in range(2**16):
  21. vals[i] = tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i)))
  22.  
  23. t = time.time()
  24. for uint in arr:
  25. vals[uint]
  26.  
  27. print("numpy and int key: %.3f" % (time.time() - t))
  28.  
  29. vals = []
  30. for i in range(2**16):
  31. vals.append(tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i))))
  32.  
  33. t = time.time()
  34. for uint in arr:
  35. vals[uint]
  36.  
  37. print("numpy and list: %.3f" % (time.time() - t))
  38.  
Success #stdin #stdout 2.37s 27792KB
stdin
Standard input is empty
stdout
slicing and byte key: 0.047
numpy and int key: 0.510
numpy and list: 0.025