comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/ma/bench.py @ 87:2a2c65a20a8b

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