Skip to content

Commit 9ea0d31

Browse files
committed
BENCH: Add benchmarks for np.non_zero
1 parent 617fe1c commit 9ea0d31

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

benchmarks/benchmarks/bench_core.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ def time_count_nonzero_multi_axis(self, numaxes, size, dtype):
169169
np.count_nonzero(self.x, axis=(
170170
self.x.ndim - 1, self.x.ndim - 2))
171171

172+
class Nonzero(Benchmark):
173+
params = [
174+
[bool, np.uint8, np.uint64, np.int64, np.float32, np.float64],
175+
[(1_000_000,), (1000, 1000), (100, ), (2, )]
176+
]
177+
param_names = ["dtype", "shape"]
178+
179+
def setup(self, dtype, size):
180+
self.x = np.random.randint(0, 3, size=size).astype(dtype)
181+
self.x_sparse = np.zeros(size=size).astype(dtype)
182+
self.x_sparse[1] = 1
183+
self.x_sparse[-1] = 1
184+
self.x_dense = np.ones(size=size).astype(dtype)
185+
186+
def time_nonzero(self, dtype, size):
187+
np.nonzero(self.x)
188+
189+
def time_nonzero_sparse(self, dtype, size):
190+
np.nonzero(self.x_sparse)
191+
192+
def time_nonzero_dense(self, dtype, size):
193+
np.nonzero(self.x_dense)
172194

173195
class PackBits(Benchmark):
174196
param_names = ['dtype']

0 commit comments

Comments
 (0)