getitem.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Benchmark for getitem
  2. from time import time
  3. import numpy as np
  4. import bcolz
  5. from bcolz.py2help import xrange
  6. N = int(1e7) # the number of elements in x
  7. M = 100000 # the elements to get
  8. clevel = 1 # the compression level
  9. print("Creating inputs with %d elements..." % N)
  10. cparams = bcolz.cparams(clevel)
  11. # x = np.arange(N)
  12. x = np.zeros(N, dtype="f8")
  13. y = x.copy()
  14. z = x.copy()
  15. cx = bcolz.carray(x, cparams=cparams)
  16. cy = cx.copy()
  17. cz = cx.copy()
  18. ct = bcolz.ctable((cx, cy, cz), names=['x', 'y', 'z'])
  19. t = ct[:]
  20. print("Starting benchmark now for getting %d elements..." % M)
  21. # Retrieve from a ndarray
  22. t0 = time()
  23. vals = [x[i] for i in xrange(0, M, 3)]
  24. print("Time for array--> %.3f" % (time() - t0,))
  25. print("vals-->", len(vals))
  26. #bcolz.set_num_threads(bcolz.ncores//2)
  27. # Retrieve from a carray
  28. t0 = time()
  29. cvals = [cx[i] for i in xrange(0, M, 3)]
  30. #cvals = cx[:M:3][:].tolist()
  31. print("Time for carray--> %.3f" % (time() - t0,))
  32. print("vals-->", len(cvals))
  33. assert vals == cvals
  34. # Retrieve from a structured ndarray
  35. t0 = time()
  36. vals = [t[i] for i in xrange(0, M, 3)]
  37. print("Time for structured array--> %.3f" % (time() - t0,))
  38. print("vals-->", len(vals))
  39. # Retrieve from a ctable
  40. t0 = time()
  41. cvals = [ct[i] for i in xrange(0, M, 3)]
  42. #cvals = ct[:M:3][:].tolist()
  43. print("Time for ctable--> %.3f" % (time() - t0,))
  44. print("vals-->", len(cvals))
  45. assert vals == cvals