Skip to content

Commit 59dd378

Browse files
authored
Merge pull request numpy#21463 from eendebakpt/benchmark_small_arrays
BENCH: Add benchmarks targeted at small arrays
2 parents e49478c + 159714b commit 59dd378

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

benchmarks/benchmarks/bench_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def time_indices(self):
207207
np.indices((1000, 500))
208208

209209
class VarComplex(Benchmark):
210-
params = [10**n for n in range(1, 9)]
210+
params = [10**n for n in range(0, 9)]
211211
def setup(self, n):
212212
self.arr = np.random.randn(n) + 1j * np.random.randn(n)
213213

benchmarks/benchmarks/bench_function_base.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ def time_weights(self):
4343
np.bincount(self.d, weights=self.e)
4444

4545

46+
class Mean(Benchmark):
47+
param_names = ['size']
48+
params = [[1, 10, 100_000]]
49+
50+
def setup(self, size):
51+
self.array = np.arange(2*size).reshape(2, size)
52+
53+
def time_mean(self, size):
54+
np.mean(self.array)
55+
56+
def time_mean_axis(self, size):
57+
np.mean(self.array, axis=1)
58+
59+
4660
class Median(Benchmark):
4761
def setup(self):
4862
self.e = np.arange(10000, dtype=np.float32)
@@ -78,14 +92,17 @@ def time_wide(self):
7892
class Percentile(Benchmark):
7993
def setup(self):
8094
self.e = np.arange(10000, dtype=np.float32)
81-
self.o = np.arange(10001, dtype=np.float32)
95+
self.o = np.arange(21, dtype=np.float32)
8296

8397
def time_quartile(self):
8498
np.percentile(self.e, [25, 75])
8599

86100
def time_percentile(self):
87101
np.percentile(self.e, [25, 35, 55, 65, 75])
88102

103+
def time_percentile_small(self):
104+
np.percentile(self.o, [25, 75])
105+
89106

90107
class Select(Benchmark):
91108
def setup(self):

benchmarks/benchmarks/bench_linalg.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ def time_op(self, op, typename):
9898
self.func(self.a)
9999

100100

101+
class LinalgSmallArrays(Benchmark):
102+
""" Test overhead of linalg methods for small arrays """
103+
def setup(self):
104+
self.array_5 = np.arange(5.)
105+
self.array_5_5 = np.arange(5.)
106+
107+
def time_norm_small_array(self):
108+
np.linalg.norm(self.array_5)
109+
110+
def time_det_small_array(self):
111+
np.linalg.det(self.array_5_5)
112+
101113
class Lstsq(Benchmark):
102114
def setup(self):
103115
self.a = get_squares_()['float64']

benchmarks/benchmarks/bench_ufunc.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,47 @@ def setup(self, ufuncname):
5757
def time_ufunc_types(self, ufuncname):
5858
[self.f(*arg) for arg in self.args]
5959

60+
class UFuncSmall(Benchmark):
61+
""" Benchmark for a selection of ufuncs on a small arrays and scalars
62+
63+
Since the arrays and scalars are small, we are benchmarking the overhead
64+
of the numpy ufunc functionality
65+
"""
66+
params = ['abs', 'sqrt', 'cos']
67+
param_names = ['ufunc']
68+
timeout = 10
69+
70+
def setup(self, ufuncname):
71+
np.seterr(all='ignore')
72+
try:
73+
self.f = getattr(np, ufuncname)
74+
except AttributeError:
75+
raise NotImplementedError()
76+
self.array_5 = np.array([1., 2., 10., 3., 4.])
77+
self.array_int_3 = np.array([1, 2, 3])
78+
self.float64 = np.float64(1.1)
79+
self.python_float = 1.1
80+
81+
def time_ufunc_small_array(self, ufuncname):
82+
self.f(self.array_5)
83+
84+
def time_ufunc_small_array_inplace(self, ufuncname):
85+
self.f(self.array_5, out = self.array_5)
86+
87+
def time_ufunc_small_int_array(self, ufuncname):
88+
self.f(self.array_int_3)
89+
90+
def time_ufunc_numpy_scalar(self, ufuncname):
91+
self.f(self.float64)
92+
93+
def time_ufunc_python_float(self, ufuncname):
94+
self.f(self.python_float)
95+
6096

6197
class Custom(Benchmark):
6298
def setup(self):
6399
self.b = np.ones(20000, dtype=bool)
100+
self.b_small = np.ones(3, dtype=bool)
64101

65102
def time_nonzero(self):
66103
np.nonzero(self.b)
@@ -74,6 +111,9 @@ def time_and_bool(self):
74111
def time_or_bool(self):
75112
(self.b | self.b)
76113

114+
def time_and_bool_small(self):
115+
(self.b_small & self.b_small)
116+
77117

78118
class CustomInplace(Benchmark):
79119
def setup(self):

0 commit comments

Comments
 (0)