eval-profile.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Benchmark to compare the times for computing expressions by using
  2. # eval() on bcolz/numpy arrays. Numexpr is needed in order to
  3. # execute this.
  4. import math
  5. from time import time
  6. import numpy as np
  7. import numexpr as ne
  8. import bcolz
  9. def compute_bcolz(sexpr, clevel, vm):
  10. # Uncomment the next for disabling threading
  11. # bcolz.set_nthreads(1)
  12. #bcolz.blosc_set_nthreads(1)
  13. print("*** bcolz (using compression clevel = %d):" % clevel)
  14. x = cx # comment this for using numpy arrays in inputs
  15. t0 = time()
  16. cout = bcolz.eval(sexpr, vm=vm, cparams=bcolz.cparams(clevel))
  17. print("Time for bcolz.eval (%s) --> %.3f" % (vm, time() - t0,))
  18. #print(", cratio (out): %.1f" % (cout.nbytes / float(cout.cbytes)))
  19. #print("cout-->", repr(cout))
  20. if __name__ == "__main__":
  21. N = 1e8 # the number of elements in x
  22. clevel = 3 # the compression level
  23. sexpr = "(x+1)<0"
  24. sexpr = "(((.25*x + .75)*x - 1.5)*x - 2)<0"
  25. # sexpr = "(((.25*x + .75)*x - 1.5)*x - 2)"
  26. doprofile = 0
  27. print("Creating inputs...")
  28. x = np.arange(N)
  29. #x = np.linspace(0,100,N)
  30. cx = bcolz.carray(x, cparams=bcolz.cparams(clevel))
  31. print("Evaluating '%s' with 10^%d points" % (sexpr, int(math.log10(N))))
  32. t0 = time()
  33. cout = ne.evaluate(sexpr)
  34. print("Time for numexpr --> %.3f" % (time() - t0,))
  35. if doprofile:
  36. import pstats
  37. import cProfile as prof
  38. prof.run('compute_bcolz(sexpr, clevel=clevel, vm="numexpr")',
  39. #prof.run('compute_bcolz(sexpr, clevel=clevel, vm="python")',
  40. 'eval.prof')
  41. stats = pstats.Stats('eval.prof')
  42. stats.strip_dirs()
  43. stats.sort_stats('time', 'calls')
  44. stats.print_stats(20)
  45. else:
  46. compute_bcolz(sexpr, clevel=clevel, vm="numexpr")
  47. #compute_bcolz(sexpr, clevel=clevel, vm="python")