|
| 1 | +#! python |
| 2 | + |
| 3 | +import timeit |
| 4 | +#import IPython.ipapi |
| 5 | +#ip = IPython.ipapi.get() |
| 6 | +#from IPython import ipmagic |
| 7 | +import numpy |
| 8 | +import maskedarray |
| 9 | +from maskedarray import filled |
| 10 | +from maskedarray.testutils import assert_equal |
| 11 | + |
| 12 | + |
| 13 | +#####--------------------------------------------------------------------------- |
| 14 | +#---- --- Global variables --- |
| 15 | +#####--------------------------------------------------------------------------- |
| 16 | + |
| 17 | +# Small arrays .................................. |
| 18 | +xs = numpy.random.uniform(-1,1,6).reshape(2,3) |
| 19 | +ys = numpy.random.uniform(-1,1,6).reshape(2,3) |
| 20 | +zs = xs + 1j * ys |
| 21 | +m1 = [[True, False, False], [False, False, True]] |
| 22 | +m2 = [[True, False, True], [False, False, True]] |
| 23 | +nmxs = numpy.ma.array(xs, mask=m1) |
| 24 | +nmys = numpy.ma.array(ys, mask=m2) |
| 25 | +nmzs = numpy.ma.array(zs, mask=m1) |
| 26 | +mmxs = maskedarray.array(xs, mask=m1) |
| 27 | +mmys = maskedarray.array(ys, mask=m2) |
| 28 | +mmzs = maskedarray.array(zs, mask=m1) |
| 29 | +# Big arrays .................................... |
| 30 | +xl = numpy.random.uniform(-1,1,100*100).reshape(100,100) |
| 31 | +yl = numpy.random.uniform(-1,1,100*100).reshape(100,100) |
| 32 | +zl = xl + 1j * yl |
| 33 | +maskx = xl > 0.8 |
| 34 | +masky = yl < -0.8 |
| 35 | +nmxl = numpy.ma.array(xl, mask=maskx) |
| 36 | +nmyl = numpy.ma.array(yl, mask=masky) |
| 37 | +nmzl = numpy.ma.array(zl, mask=maskx) |
| 38 | +mmxl = maskedarray.array(xl, mask=maskx, shrink=True) |
| 39 | +mmyl = maskedarray.array(yl, mask=masky, shrink=True) |
| 40 | +mmzl = maskedarray.array(zl, mask=maskx, shrink=True) |
| 41 | + |
| 42 | +#####--------------------------------------------------------------------------- |
| 43 | +#---- --- Functions --- |
| 44 | +#####--------------------------------------------------------------------------- |
| 45 | + |
| 46 | +def timer(s, v='', nloop=500, nrep=3): |
| 47 | + units = ["s", "ms", "\xb5s", "ns"] |
| 48 | + scaling = [1, 1e3, 1e6, 1e9] |
| 49 | + print "%s : %-50s : " % (v,s), |
| 50 | + varnames = ["%ss,nm%ss,mm%ss,%sl,nm%sl,mm%sl" % tuple(x*6) for x in 'xyz'] |
| 51 | + setup = 'from __main__ import numpy, maskedarray, %s' % ','.join(varnames) |
| 52 | + Timer = timeit.Timer(stmt=s, setup=setup) |
| 53 | + best = min(Timer.repeat(nrep, nloop)) / nloop |
| 54 | + if best > 0.0: |
| 55 | + order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3) |
| 56 | + else: |
| 57 | + order = 3 |
| 58 | + print "%d loops, best of %d: %.*g %s per loop" % (nloop, nrep, |
| 59 | + 3, |
| 60 | + best * scaling[order], |
| 61 | + units[order]) |
| 62 | +# ip.magic('timeit -n%i %s' % (nloop,s)) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +def compare_functions_1v(func, nloop=500, test=True, |
| 67 | + xs=xs, nmxs=nmxs, mmxs=mmxs, |
| 68 | + xl=xl, nmxl=nmxl, mmxl=mmxl): |
| 69 | + funcname = func.__name__ |
| 70 | + print "-"*50 |
| 71 | + print "%s on small arrays" % funcname |
| 72 | + if test: |
| 73 | + assert_equal(filled(eval("numpy.ma.%s(nmxs)" % funcname),0), |
| 74 | + filled(eval("maskedarray.%s(mmxs)" % funcname),0)) |
| 75 | + for (module, data) in zip(("numpy", "numpy.ma","maskedarray"), |
| 76 | + ("xs","nmxs","mmxs")): |
| 77 | + timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) |
| 78 | + # |
| 79 | + print "%s on large arrays" % funcname |
| 80 | + if test: |
| 81 | + assert_equal(filled(eval("numpy.ma.%s(nmxl)" % funcname),0), |
| 82 | + filled(eval("maskedarray.%s(mmxl)" % funcname),0)) |
| 83 | + for (module, data) in zip(("numpy", "numpy.ma","maskedarray"), |
| 84 | + ("xl","nmxl","mmxl")): |
| 85 | + timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) |
| 86 | + return |
| 87 | + |
| 88 | +def compare_methods(methodname, args, vars='x', nloop=500, test=True, |
| 89 | + xs=xs, nmxs=nmxs, mmxs=mmxs, |
| 90 | + xl=xl, nmxl=nmxl, mmxl=mmxl): |
| 91 | + print "-"*50 |
| 92 | + print "%s on small arrays" % methodname |
| 93 | + if test: |
| 94 | + assert_equal(filled(eval("nm%ss.%s(%s)" % (vars,methodname,args)),0), |
| 95 | + filled(eval("mm%ss.%s(%s)" % (vars,methodname,args)),0)) |
| 96 | + for (data, ver) in zip(["nm%ss" % vars, "mm%ss" % vars], ('numpy.ma ','maskedarray')): |
| 97 | + timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop) |
| 98 | + # |
| 99 | + print "%s on large arrays" % methodname |
| 100 | + if test: |
| 101 | + assert_equal(filled(eval("nm%sl.%s(%s)" % (vars,methodname,args)),0), |
| 102 | + filled(eval("mm%sl.%s(%s)" % (vars,methodname,args)),0)) |
| 103 | + for (data, ver) in zip(["nm%sl" % vars, "mm%sl" % vars], ('numpy.ma ','maskedarray')): |
| 104 | + timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop) |
| 105 | + return |
| 106 | + |
| 107 | +def compare_functions_2v(func, nloop=500, test=True, |
| 108 | + xs=xs, nmxs=nmxs, mmxs=mmxs, |
| 109 | + ys=ys, nmys=nmys, mmys=mmys, |
| 110 | + xl=xl, nmxl=nmxl, mmxl=mmxl, |
| 111 | + yl=yl, nmyl=nmyl, mmyl=mmyl): |
| 112 | + funcname = func.__name__ |
| 113 | + print "-"*50 |
| 114 | + print "%s on small arrays" % funcname |
| 115 | + if test: |
| 116 | + assert_equal(filled(eval("numpy.ma.%s(nmxs,nmys)" % funcname),0), |
| 117 | + filled(eval("maskedarray.%s(mmxs,mmys)" % funcname),0)) |
| 118 | + for (module, data) in zip(("numpy", "numpy.ma","maskedarray"), |
| 119 | + ("xs,ys","nmxs,nmys","mmxs,mmys")): |
| 120 | + timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) |
| 121 | + # |
| 122 | + print "%s on large arrays" % funcname |
| 123 | + if test: |
| 124 | + assert_equal(filled(eval("numpy.ma.%s(nmxl, nmyl)" % funcname),0), |
| 125 | + filled(eval("maskedarray.%s(mmxl, mmyl)" % funcname),0)) |
| 126 | + for (module, data) in zip(("numpy", "numpy.ma","maskedarray"), |
| 127 | + ("xl,yl","nmxl,nmyl","mmxl,mmyl")): |
| 128 | + timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop) |
| 129 | + return |
| 130 | + |
| 131 | + |
| 132 | +############################################################################### |
| 133 | + |
| 134 | + |
| 135 | +################################################################################ |
| 136 | +if __name__ == '__main__': |
| 137 | +# # Small arrays .................................. |
| 138 | +# xs = numpy.random.uniform(-1,1,6).reshape(2,3) |
| 139 | +# ys = numpy.random.uniform(-1,1,6).reshape(2,3) |
| 140 | +# zs = xs + 1j * ys |
| 141 | +# m1 = [[True, False, False], [False, False, True]] |
| 142 | +# m2 = [[True, False, True], [False, False, True]] |
| 143 | +# nmxs = numpy.ma.array(xs, mask=m1) |
| 144 | +# nmys = numpy.ma.array(ys, mask=m2) |
| 145 | +# nmzs = numpy.ma.array(zs, mask=m1) |
| 146 | +# mmxs = maskedarray.array(xs, mask=m1) |
| 147 | +# mmys = maskedarray.array(ys, mask=m2) |
| 148 | +# mmzs = maskedarray.array(zs, mask=m1) |
| 149 | +# # Big arrays .................................... |
| 150 | +# xl = numpy.random.uniform(-1,1,100*100).reshape(100,100) |
| 151 | +# yl = numpy.random.uniform(-1,1,100*100).reshape(100,100) |
| 152 | +# zl = xl + 1j * yl |
| 153 | +# maskx = xl > 0.8 |
| 154 | +# masky = yl < -0.8 |
| 155 | +# nmxl = numpy.ma.array(xl, mask=maskx) |
| 156 | +# nmyl = numpy.ma.array(yl, mask=masky) |
| 157 | +# nmzl = numpy.ma.array(zl, mask=maskx) |
| 158 | +# mmxl = maskedarray.array(xl, mask=maskx, shrink=True) |
| 159 | +# mmyl = maskedarray.array(yl, mask=masky, shrink=True) |
| 160 | +# mmzl = maskedarray.array(zl, mask=maskx, shrink=True) |
| 161 | +# |
| 162 | + compare_functions_1v(numpy.sin) |
| 163 | + compare_functions_1v(numpy.log) |
| 164 | + compare_functions_1v(numpy.sqrt) |
| 165 | + #.................................................................... |
| 166 | + compare_functions_2v(numpy.multiply) |
| 167 | + compare_functions_2v(numpy.divide) |
| 168 | + compare_functions_2v(numpy.power) |
| 169 | + #.................................................................... |
| 170 | + compare_methods('ravel','', nloop=1000) |
| 171 | + compare_methods('conjugate','','z', nloop=1000) |
| 172 | + compare_methods('transpose','', nloop=1000) |
| 173 | + compare_methods('compressed','', nloop=1000) |
| 174 | + compare_methods('__getitem__','0', nloop=1000) |
| 175 | + compare_methods('__getitem__','(0,0)', nloop=1000) |
| 176 | + compare_methods('__getitem__','[0,-1]', nloop=1000) |
| 177 | + compare_methods('__setitem__','0, 17', nloop=1000, test=False) |
| 178 | + compare_methods('__setitem__','(0,0), 17', nloop=1000, test=False) |
| 179 | + #.................................................................... |
| 180 | + print "-"*50 |
| 181 | + print "__setitem__ on small arrays" |
| 182 | + timer('nmxs.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ',nloop=10000) |
| 183 | + timer('mmxs.__setitem__((-1,0),maskedarray.masked)', 'maskedarray',nloop=10000) |
| 184 | + print "-"*50 |
| 185 | + print "__setitem__ on large arrays" |
| 186 | + timer('nmxl.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ',nloop=10000) |
| 187 | + timer('mmxl.__setitem__((-1,0),maskedarray.masked)', 'maskedarray',nloop=10000) |
| 188 | + #.................................................................... |
| 189 | + print "-"*50 |
| 190 | + print "where on small arrays" |
| 191 | + assert_equal(eval("numpy.ma.where(nmxs>2,nmxs,nmys)"), |
| 192 | + eval("maskedarray.where(mmxs>2, mmxs,mmys)")) |
| 193 | + timer('numpy.ma.where(nmxs>2,nmxs,nmys)', 'numpy.ma ',nloop=1000) |
| 194 | + timer('maskedarray.where(mmxs>2, mmxs,mmys)', 'maskedarray',nloop=1000) |
| 195 | + print "-"*50 |
| 196 | + print "where on large arrays" |
| 197 | + timer('numpy.ma.where(nmxl>2,nmxl,nmyl)', 'numpy.ma ',nloop=100) |
| 198 | + timer('maskedarray.where(mmxl>2, mmxl,mmyl)', 'maskedarray',nloop=100) |
0 commit comments