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