Chris@87: #! python Chris@87: # encoding: utf-8 Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: import timeit Chris@87: #import IPython.ipapi Chris@87: #ip = IPython.ipapi.get() Chris@87: #from IPython import ipmagic Chris@87: import numpy Chris@87: #from numpy import ma Chris@87: #from numpy.ma import filled Chris@87: #from numpy.ma.testutils import assert_equal Chris@87: Chris@87: Chris@87: #####--------------------------------------------------------------------------- Chris@87: #---- --- Global variables --- Chris@87: #####--------------------------------------------------------------------------- Chris@87: Chris@87: # Small arrays .................................. Chris@87: xs = numpy.random.uniform(-1, 1, 6).reshape(2, 3) Chris@87: ys = numpy.random.uniform(-1, 1, 6).reshape(2, 3) Chris@87: zs = xs + 1j * ys Chris@87: m1 = [[True, False, False], [False, False, True]] Chris@87: m2 = [[True, False, True], [False, False, True]] Chris@87: nmxs = numpy.ma.array(xs, mask=m1) Chris@87: nmys = numpy.ma.array(ys, mask=m2) Chris@87: nmzs = numpy.ma.array(zs, mask=m1) Chris@87: # Big arrays .................................... Chris@87: xl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100) Chris@87: yl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100) Chris@87: zl = xl + 1j * yl Chris@87: maskx = xl > 0.8 Chris@87: masky = yl < -0.8 Chris@87: nmxl = numpy.ma.array(xl, mask=maskx) Chris@87: nmyl = numpy.ma.array(yl, mask=masky) Chris@87: nmzl = numpy.ma.array(zl, mask=maskx) Chris@87: Chris@87: #####--------------------------------------------------------------------------- Chris@87: #---- --- Functions --- Chris@87: #####--------------------------------------------------------------------------- Chris@87: Chris@87: def timer(s, v='', nloop=500, nrep=3): Chris@87: units = ["s", "ms", "µs", "ns"] Chris@87: scaling = [1, 1e3, 1e6, 1e9] Chris@87: print("%s : %-50s : " % (v, s), end=' ') Chris@87: varnames = ["%ss,nm%ss,%sl,nm%sl" % tuple(x*4) for x in 'xyz'] Chris@87: setup = 'from __main__ import numpy, ma, %s' % ','.join(varnames) Chris@87: Timer = timeit.Timer(stmt=s, setup=setup) Chris@87: best = min(Timer.repeat(nrep, nloop)) / nloop Chris@87: if best > 0.0: Chris@87: order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3) Chris@87: else: Chris@87: order = 3 Chris@87: print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep, Chris@87: 3, Chris@87: best * scaling[order], Chris@87: units[order])) Chris@87: # ip.magic('timeit -n%i %s' % (nloop,s)) Chris@87: Chris@87: Chris@87: Chris@87: def compare_functions_1v(func, nloop=500, Chris@87: xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl): Chris@87: funcname = func.__name__ Chris@87: print("-"*50) Chris@87: print("%s on small arrays" % funcname) Chris@87: module, data = "numpy.ma", "nmxs" Chris@87: timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) Chris@87: # Chris@87: print("%s on large arrays" % funcname) Chris@87: module, data = "numpy.ma", "nmxl" Chris@87: timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) Chris@87: return Chris@87: Chris@87: def compare_methods(methodname, args, vars='x', nloop=500, test=True, Chris@87: xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl): Chris@87: print("-"*50) Chris@87: print("%s on small arrays" % methodname) Chris@87: data, ver = "nm%ss" % vars, 'numpy.ma' Chris@87: timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop) Chris@87: # Chris@87: print("%s on large arrays" % methodname) Chris@87: data, ver = "nm%sl" % vars, 'numpy.ma' Chris@87: timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop) Chris@87: return Chris@87: Chris@87: def compare_functions_2v(func, nloop=500, test=True, Chris@87: xs=xs, nmxs=nmxs, Chris@87: ys=ys, nmys=nmys, Chris@87: xl=xl, nmxl=nmxl, Chris@87: yl=yl, nmyl=nmyl): Chris@87: funcname = func.__name__ Chris@87: print("-"*50) Chris@87: print("%s on small arrays" % funcname) Chris@87: module, data = "numpy.ma", "nmxs,nmys" Chris@87: timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) Chris@87: # Chris@87: print("%s on large arrays" % funcname) Chris@87: module, data = "numpy.ma", "nmxl,nmyl" Chris@87: timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) Chris@87: return Chris@87: Chris@87: Chris@87: ############################################################################### Chris@87: Chris@87: Chris@87: ################################################################################ Chris@87: if __name__ == '__main__': Chris@87: # # Small arrays .................................. Chris@87: # xs = numpy.random.uniform(-1,1,6).reshape(2,3) Chris@87: # ys = numpy.random.uniform(-1,1,6).reshape(2,3) Chris@87: # zs = xs + 1j * ys Chris@87: # m1 = [[True, False, False], [False, False, True]] Chris@87: # m2 = [[True, False, True], [False, False, True]] Chris@87: # nmxs = numpy.ma.array(xs, mask=m1) Chris@87: # nmys = numpy.ma.array(ys, mask=m2) Chris@87: # nmzs = numpy.ma.array(zs, mask=m1) Chris@87: # mmxs = maskedarray.array(xs, mask=m1) Chris@87: # mmys = maskedarray.array(ys, mask=m2) Chris@87: # mmzs = maskedarray.array(zs, mask=m1) Chris@87: # # Big arrays .................................... Chris@87: # xl = numpy.random.uniform(-1,1,100*100).reshape(100,100) Chris@87: # yl = numpy.random.uniform(-1,1,100*100).reshape(100,100) Chris@87: # zl = xl + 1j * yl Chris@87: # maskx = xl > 0.8 Chris@87: # masky = yl < -0.8 Chris@87: # nmxl = numpy.ma.array(xl, mask=maskx) Chris@87: # nmyl = numpy.ma.array(yl, mask=masky) Chris@87: # nmzl = numpy.ma.array(zl, mask=maskx) Chris@87: # mmxl = maskedarray.array(xl, mask=maskx, shrink=True) Chris@87: # mmyl = maskedarray.array(yl, mask=masky, shrink=True) Chris@87: # mmzl = maskedarray.array(zl, mask=maskx, shrink=True) Chris@87: # Chris@87: compare_functions_1v(numpy.sin) Chris@87: compare_functions_1v(numpy.log) Chris@87: compare_functions_1v(numpy.sqrt) Chris@87: #.................................................................... Chris@87: compare_functions_2v(numpy.multiply) Chris@87: compare_functions_2v(numpy.divide) Chris@87: compare_functions_2v(numpy.power) Chris@87: #.................................................................... Chris@87: compare_methods('ravel', '', nloop=1000) Chris@87: compare_methods('conjugate', '', 'z', nloop=1000) Chris@87: compare_methods('transpose', '', nloop=1000) Chris@87: compare_methods('compressed', '', nloop=1000) Chris@87: compare_methods('__getitem__', '0', nloop=1000) Chris@87: compare_methods('__getitem__', '(0,0)', nloop=1000) Chris@87: compare_methods('__getitem__', '[0,-1]', nloop=1000) Chris@87: compare_methods('__setitem__', '0, 17', nloop=1000, test=False) Chris@87: compare_methods('__setitem__', '(0,0), 17', nloop=1000, test=False) Chris@87: #.................................................................... Chris@87: print("-"*50) Chris@87: print("__setitem__ on small arrays") Chris@87: timer('nmxs.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000) Chris@87: Chris@87: print("-"*50) Chris@87: print("__setitem__ on large arrays") Chris@87: timer('nmxl.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000) Chris@87: Chris@87: #.................................................................... Chris@87: print("-"*50) Chris@87: print("where on small arrays") Chris@87: timer('numpy.ma.where(nmxs>2,nmxs,nmys)', 'numpy.ma ', nloop=1000) Chris@87: print("-"*50) Chris@87: print("where on large arrays") Chris@87: timer('numpy.ma.where(nmxl>2,nmxl,nmyl)', 'numpy.ma ', nloop=100)