bench.py 4.8 KB

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