Skip to content

Commit c195609

Browse files
authored
Merge pull request numpy#27517 from eendebakpt/nonzero_benchmarks
BENCH: Add benchmarks for np.non_zero
2 parents 617fe1c + f615671 commit c195609

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

benchmarks/benchmarks/bench_core.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,34 @@ def time_count_nonzero_multi_axis(self, numaxes, size, dtype):
170170
self.x.ndim - 1, self.x.ndim - 2))
171171

172172

173+
class Nonzero(Benchmark):
174+
params = [
175+
[bool, np.uint8, np.uint64, np.int64, np.float32, np.float64],
176+
[(1_000_000,), (1000, 1000), (100, ), (2, )]
177+
]
178+
param_names = ["dtype", "shape"]
179+
180+
def setup(self, dtype, size):
181+
self.x = np.random.randint(0, 3, size=size).astype(dtype)
182+
self.x_sparse = np.zeros(size).astype(dtype)
183+
self.x_sparse[1] = 1
184+
self.x_sparse[-1] = 1
185+
self.x_dense = np.ones(size).astype(dtype)
186+
187+
def time_nonzero(self, dtype, size):
188+
np.nonzero(self.x)
189+
190+
def time_nonzero_sparse(self, dtype, size):
191+
np.nonzero(self.x_sparse)
192+
193+
def time_nonzero_dense(self, dtype, size):
194+
np.nonzero(self.x_dense)
195+
196+
173197
class PackBits(Benchmark):
174198
param_names = ['dtype']
175199
params = [[bool, np.uintp]]
200+
176201
def setup(self, dtype):
177202
self.d = np.ones(10000, dtype=dtype)
178203
self.d2 = np.ones((200, 1000), dtype=dtype)

0 commit comments

Comments
 (0)