Skip to content

Commit 86fcce6

Browse files
authored
BENCH: Add basic benchmarks for scalar indexing and assignment (numpy#16786)
* BENCH: Add basic benchmarks for scalar indexing and assignment
1 parent 996e641 commit 86fcce6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

benchmarks/benchmarks/bench_indexing.py

+30
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ def time_op(self, indexes, sel, op):
3131
self.func()
3232

3333

34+
class ScalarIndexing(Benchmark):
35+
params = [[0, 1, 2]]
36+
param_names = ["ndim"]
37+
38+
def setup(self, ndim):
39+
self.array = np.ones((5,) * ndim)
40+
41+
def time_index(self, ndim):
42+
# time indexing.
43+
arr = self.array
44+
indx = (1,) * ndim
45+
for i in range(100):
46+
arr[indx]
47+
48+
def time_assign(self, ndim):
49+
# time assignment from a python scalar
50+
arr = self.array
51+
indx = (1,) * ndim
52+
for i in range(100):
53+
arr[indx] = 5.
54+
55+
def time_assign_cast(self, ndim):
56+
# time an assignment which may use a cast operation
57+
arr = self.array
58+
indx = (1,) * ndim
59+
val = np.int16(43)
60+
for i in range(100):
61+
arr[indx] = val
62+
63+
3464
class IndexingSeparate(Benchmark):
3565
def setup(self):
3666
self.tmp_dir = mkdtemp()

0 commit comments

Comments
 (0)