Skip to content

Commit 78a1837

Browse files
committed
BENCH: Add some basic ma benchmarks
1 parent d5cc7cd commit 78a1837

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

benchmarks/benchmarks/bench_ma.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,96 @@ def time_masked_array_l100(self):
1818

1919
def time_masked_array_l100_t100(self):
2020
np.ma.masked_array(self.l100, self.t100)
21+
22+
23+
class Indexing(Benchmark):
24+
param_names = ['masked', 'ndim', 'size']
25+
params = [[True, False],
26+
[1, 2],
27+
[10, 100, 1000]]
28+
def setup(self, masked, ndim, size):
29+
x = np.arange(size**ndim).reshape(ndim * (size,))
30+
31+
if masked:
32+
self.m = np.ma.array(x, mask=x%2 == 0)
33+
else:
34+
self.m = np.ma.array(x)
35+
36+
self.idx_scalar = (size//2,) * ndim
37+
self.idx_0d = (size//2,) * ndim + (Ellipsis,)
38+
self.idx_1d = (size//2,) * (ndim - 1)
39+
40+
def time_scalar(self, masked, ndim, size):
41+
self.m[self.idx_scalar]
42+
43+
def time_0d(self, masked, ndim, size):
44+
self.m[self.idx_0d]
45+
46+
def time_1d(self, masked, ndim, size):
47+
self.m[self.idx_1d]
48+
49+
50+
class UFunc(Benchmark):
51+
param_names = ['a_masked', 'b_masked', 'size']
52+
params = [[True, False],
53+
[True, False],
54+
[10, 100, 1000]]
55+
56+
def setup(self, a_masked, b_masked, size):
57+
x = np.arange(size).astype(np.uint8)
58+
59+
self.a_scalar = np.ma.masked if a_masked else 5
60+
self.b_scalar = np.ma.masked if b_masked else 3
61+
62+
self.a_1d = np.ma.array(x, mask=x%2 == 0 if a_masked else np.ma.nomask)
63+
self.b_1d = np.ma.array(x, mask=x%3 == 0 if b_masked else np.ma.nomask)
64+
65+
self.a_2d = self.a_1d.reshape(1, -1)
66+
self.b_2d = self.a_1d.reshape(-1, 1)
67+
68+
def time_scalar(self, a_masked, b_masked, size):
69+
np.ma.add(self.a_scalar, self.b_scalar)
70+
71+
def time_scalar_1d(self, a_masked, b_masked, size):
72+
np.ma.add(self.a_scalar, self.b_1d)
73+
74+
def time_1d(self, a_masked, b_masked, size):
75+
np.ma.add(self.a_1d, self.b_1d)
76+
77+
def time_2d(self, a_masked, b_masked, size):
78+
# broadcasting happens this time
79+
np.ma.add(self.a_2d, self.b_2d)
80+
81+
82+
class Concatenate(Benchmark):
83+
param_names = ['mode', 'n']
84+
params = [
85+
['ndarray', 'unmasked',
86+
'ndarray+masked', 'unmasked+masked',
87+
'masked'],
88+
[2, 100, 2000]
89+
]
90+
91+
def setup(self, mode, n):
92+
normal = np.zeros((n, n), int)
93+
unmasked = np.ma.zeros((n, n), int)
94+
masked = np.ma.array(normal, mask=True)
95+
96+
mode_parts = mode.split('+')
97+
base = mode_parts[0]
98+
promote = 'masked' in mode_parts[1:]
99+
100+
if base == 'ndarray':
101+
args = 10 * (normal,)
102+
elif base == 'unmasked':
103+
args = 10 * (unmasked,)
104+
else:
105+
args = 10 * (masked,)
106+
107+
if promote:
108+
args = args[:-1] + (masked,)
109+
110+
self.args = args
111+
112+
def time_it(self, mode, n):
113+
np.ma.concatenate(self.args)

0 commit comments

Comments
 (0)