Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/ma/bench.py
7757 views
1
#!/usr/bin/env python3
2
3
import timeit
4
import numpy
5
6
7
###############################################################################
8
# Global variables #
9
###############################################################################
10
11
12
# Small arrays
13
xs = numpy.random.uniform(-1, 1, 6).reshape(2, 3)
14
ys = numpy.random.uniform(-1, 1, 6).reshape(2, 3)
15
zs = xs + 1j * ys
16
m1 = [[True, False, False], [False, False, True]]
17
m2 = [[True, False, True], [False, False, True]]
18
nmxs = numpy.ma.array(xs, mask=m1)
19
nmys = numpy.ma.array(ys, mask=m2)
20
nmzs = numpy.ma.array(zs, mask=m1)
21
22
# Big arrays
23
xl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)
24
yl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)
25
zl = xl + 1j * yl
26
maskx = xl > 0.8
27
masky = yl < -0.8
28
nmxl = numpy.ma.array(xl, mask=maskx)
29
nmyl = numpy.ma.array(yl, mask=masky)
30
nmzl = numpy.ma.array(zl, mask=maskx)
31
32
33
###############################################################################
34
# Functions #
35
###############################################################################
36
37
38
def timer(s, v='', nloop=500, nrep=3):
39
units = ["s", "ms", "µs", "ns"]
40
scaling = [1, 1e3, 1e6, 1e9]
41
print("%s : %-50s : " % (v, s), end=' ')
42
varnames = ["%ss,nm%ss,%sl,nm%sl" % tuple(x*4) for x in 'xyz']
43
setup = 'from __main__ import numpy, ma, %s' % ','.join(varnames)
44
Timer = timeit.Timer(stmt=s, setup=setup)
45
best = min(Timer.repeat(nrep, nloop)) / nloop
46
if best > 0.0:
47
order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3)
48
else:
49
order = 3
50
print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep,
51
3,
52
best * scaling[order],
53
units[order]))
54
55
56
def compare_functions_1v(func, nloop=500,
57
xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
58
funcname = func.__name__
59
print("-"*50)
60
print(f'{funcname} on small arrays')
61
module, data = "numpy.ma", "nmxs"
62
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
63
64
print("%s on large arrays" % funcname)
65
module, data = "numpy.ma", "nmxl"
66
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
67
return
68
69
def compare_methods(methodname, args, vars='x', nloop=500, test=True,
70
xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
71
print("-"*50)
72
print(f'{methodname} on small arrays')
73
data, ver = f'nm{vars}l', 'numpy.ma'
74
timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
75
76
print("%s on large arrays" % methodname)
77
data, ver = "nm%sl" % vars, 'numpy.ma'
78
timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
79
return
80
81
def compare_functions_2v(func, nloop=500, test=True,
82
xs=xs, nmxs=nmxs,
83
ys=ys, nmys=nmys,
84
xl=xl, nmxl=nmxl,
85
yl=yl, nmyl=nmyl):
86
funcname = func.__name__
87
print("-"*50)
88
print(f'{funcname} on small arrays')
89
module, data = "numpy.ma", "nmxs,nmys"
90
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
91
92
print(f'{funcname} on large arrays')
93
module, data = "numpy.ma", "nmxl,nmyl"
94
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
95
return
96
97
98
if __name__ == '__main__':
99
compare_functions_1v(numpy.sin)
100
compare_functions_1v(numpy.log)
101
compare_functions_1v(numpy.sqrt)
102
103
compare_functions_2v(numpy.multiply)
104
compare_functions_2v(numpy.divide)
105
compare_functions_2v(numpy.power)
106
107
compare_methods('ravel', '', nloop=1000)
108
compare_methods('conjugate', '', 'z', nloop=1000)
109
compare_methods('transpose', '', nloop=1000)
110
compare_methods('compressed', '', nloop=1000)
111
compare_methods('__getitem__', '0', nloop=1000)
112
compare_methods('__getitem__', '(0,0)', nloop=1000)
113
compare_methods('__getitem__', '[0,-1]', nloop=1000)
114
compare_methods('__setitem__', '0, 17', nloop=1000, test=False)
115
compare_methods('__setitem__', '(0,0), 17', nloop=1000, test=False)
116
117
print("-"*50)
118
print("__setitem__ on small arrays")
119
timer('nmxs.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)
120
121
print("-"*50)
122
print("__setitem__ on large arrays")
123
timer('nmxl.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)
124
125
print("-"*50)
126
print("where on small arrays")
127
timer('numpy.ma.where(nmxs>2,nmxs,nmys)', 'numpy.ma ', nloop=1000)
128
print("-"*50)
129
print("where on large arrays")
130
timer('numpy.ma.where(nmxl>2,nmxl,nmyl)', 'numpy.ma ', nloop=100)
131
132