iterator.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Benchmark for iterators
  2. from time import time
  3. import numpy as np
  4. import bcolz
  5. N = 1e8 # the number of elements in x
  6. clevel = 5 # the compression level
  7. sexpr = "(x-1) < 10." # the expression to compute
  8. # sexpr = "((x-1) % 1000) == 0." # the expression to compute
  9. #sexpr = "(2*x**3+.3*y**2+z+1)<0" # the expression to compute
  10. cparams = bcolz.cparams(clevel)
  11. print("Creating inputs...")
  12. x = np.arange(N)
  13. cx = bcolz.carray(x, cparams=cparams)
  14. if 'y' not in sexpr:
  15. ct = bcolz.ctable((cx,), names=['x'])
  16. else:
  17. y = np.arange(N)
  18. z = np.arange(N)
  19. cy = bcolz.carray(y, cparams=cparams)
  20. cz = bcolz.carray(z, cparams=cparams)
  21. ct = bcolz.ctable((cx, cy, cz), names=['x', 'y', 'z'])
  22. print("Evaluating...", sexpr)
  23. t0 = time()
  24. cbout = ct.eval(sexpr)
  25. print("Time for evaluation--> %.3f" % (time() - t0,))
  26. print("Converting to numy arrays")
  27. bout = cbout[:]
  28. t = ct[:]
  29. t0 = time()
  30. cbool = bcolz.carray(bout, cparams=cparams)
  31. print("Time for converting boolean--> %.3f" % (time() - t0,))
  32. print("cbool-->", repr(cbool))
  33. t0 = time()
  34. vals = [v for v in cbool.wheretrue()]
  35. print("Time for wheretrue()--> %.3f" % (time() - t0,))
  36. print("vals-->", len(vals))
  37. print("Starting benchmark now...")
  38. # Retrieve from a ndarray
  39. t0 = time()
  40. vals = [v for v in x[bout]]
  41. print("Time for array--> %.3f" % (time() - t0,))
  42. #print("vals-->", len(vals))
  43. #bcolz.set_num_threads(bcolz.ncores//2)
  44. # Retrieve from a carray
  45. t0 = time()
  46. #cvals = [v for v in cx[cbout]]
  47. cvals = [v for v in cx.where(cbout)]
  48. print("Time for carray--> %.3f" % (time() - t0,))
  49. #print("vals-->", len(cvals))
  50. assert vals == cvals
  51. # Retrieve from a structured ndarray
  52. t0 = time()
  53. vals = [tuple(v) for v in t[bout]]
  54. print("Time for structured array--> %.3f" % (time() - t0,))
  55. #print("vals-->", len(vals))
  56. # Retrieve from a ctable
  57. t0 = time()
  58. #cvals = [tuple(v) for v in ct[cbout]]
  59. cvals = [v for v in ct.where(cbout)]
  60. print("Time for ctable--> %.3f" % (time() - t0,))
  61. #print("vals-->", len(cvals))
  62. assert vals == cvals