bench.py 4.8 KB

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