bench.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python3
  2. import timeit
  3. import numpy
  4. ###############################################################################
  5. # Global variables #
  6. ###############################################################################
  7. # Small arrays
  8. xs = numpy.random.uniform(-1, 1, 6).reshape(2, 3)
  9. ys = numpy.random.uniform(-1, 1, 6).reshape(2, 3)
  10. zs = xs + 1j * ys
  11. m1 = [[True, False, False], [False, False, True]]
  12. m2 = [[True, False, True], [False, False, True]]
  13. nmxs = numpy.ma.array(xs, mask=m1)
  14. nmys = numpy.ma.array(ys, mask=m2)
  15. nmzs = numpy.ma.array(zs, mask=m1)
  16. # Big arrays
  17. xl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)
  18. yl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)
  19. zl = xl + 1j * yl
  20. maskx = xl > 0.8
  21. masky = yl < -0.8
  22. nmxl = numpy.ma.array(xl, mask=maskx)
  23. nmyl = numpy.ma.array(yl, mask=masky)
  24. nmzl = numpy.ma.array(zl, mask=maskx)
  25. ###############################################################################
  26. # Functions #
  27. ###############################################################################
  28. def timer(s, v='', nloop=500, nrep=3):
  29. units = ["s", "ms", "µs", "ns"]
  30. scaling = [1, 1e3, 1e6, 1e9]
  31. print("%s : %-50s : " % (v, s), end=' ')
  32. varnames = ["%ss,nm%ss,%sl,nm%sl" % tuple(x*4) for x in 'xyz']
  33. setup = 'from __main__ import numpy, ma, %s' % ','.join(varnames)
  34. Timer = timeit.Timer(stmt=s, setup=setup)
  35. best = min(Timer.repeat(nrep, nloop)) / nloop
  36. if best > 0.0:
  37. order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3)
  38. else:
  39. order = 3
  40. print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep,
  41. 3,
  42. best * scaling[order],
  43. units[order]))
  44. def compare_functions_1v(func, nloop=500,
  45. xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
  46. funcname = func.__name__
  47. print("-"*50)
  48. print(f'{funcname} on small arrays')
  49. module, data = "numpy.ma", "nmxs"
  50. timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
  51. print("%s on large arrays" % funcname)
  52. module, data = "numpy.ma", "nmxl"
  53. timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
  54. return
  55. def compare_methods(methodname, args, vars='x', nloop=500, test=True,
  56. xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
  57. print("-"*50)
  58. print(f'{methodname} on small arrays')
  59. data, ver = f'nm{vars}l', 'numpy.ma'
  60. timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
  61. print("%s on large arrays" % methodname)
  62. data, ver = "nm%sl" % vars, 'numpy.ma'
  63. timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
  64. return
  65. def compare_functions_2v(func, nloop=500, test=True,
  66. xs=xs, nmxs=nmxs,
  67. ys=ys, nmys=nmys,
  68. xl=xl, nmxl=nmxl,
  69. yl=yl, nmyl=nmyl):
  70. funcname = func.__name__
  71. print("-"*50)
  72. print(f'{funcname} on small arrays')
  73. module, data = "numpy.ma", "nmxs,nmys"
  74. timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
  75. print(f'{funcname} on large arrays')
  76. module, data = "numpy.ma", "nmxl,nmyl"
  77. timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
  78. return
  79. if __name__ == '__main__':
  80. compare_functions_1v(numpy.sin)
  81. compare_functions_1v(numpy.log)
  82. compare_functions_1v(numpy.sqrt)
  83. compare_functions_2v(numpy.multiply)
  84. compare_functions_2v(numpy.divide)
  85. compare_functions_2v(numpy.power)
  86. compare_methods('ravel', '', nloop=1000)
  87. compare_methods('conjugate', '', 'z', nloop=1000)
  88. compare_methods('transpose', '', nloop=1000)
  89. compare_methods('compressed', '', nloop=1000)
  90. compare_methods('__getitem__', '0', nloop=1000)
  91. compare_methods('__getitem__', '(0,0)', nloop=1000)
  92. compare_methods('__getitem__', '[0,-1]', nloop=1000)
  93. compare_methods('__setitem__', '0, 17', nloop=1000, test=False)
  94. compare_methods('__setitem__', '(0,0), 17', nloop=1000, test=False)
  95. print("-"*50)
  96. print("__setitem__ on small arrays")
  97. timer('nmxs.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)
  98. print("-"*50)
  99. print("__setitem__ on large arrays")
  100. timer('nmxl.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)
  101. print("-"*50)
  102. print("where on small arrays")
  103. timer('numpy.ma.where(nmxs>2,nmxs,nmys)', 'numpy.ma ', nloop=1000)
  104. print("-"*50)
  105. print("where on large arrays")
  106. timer('numpy.ma.where(nmxl>2,nmxl,nmyl)', 'numpy.ma ', nloop=100)