Skip to content

Commit 22e8dd1

Browse files
authored
add benchmark for class / method calls (#4016)
1 parent 74d9d23 commit 22e8dd1

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

pytests/src/pyclasses.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ impl EmptyClass {
1111
fn new() -> Self {
1212
EmptyClass {}
1313
}
14+
15+
fn method(&self) {}
16+
17+
fn __len__(&self) -> usize {
18+
0
19+
}
1420
}
1521

1622
/// This is for demonstrating how to return a value from __next__

pytests/tests/test_pyclasses.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,38 @@ def test_empty_class_init(benchmark):
88
benchmark(pyclasses.EmptyClass)
99

1010

11+
def test_method_call(benchmark):
12+
obj = pyclasses.EmptyClass()
13+
assert benchmark(obj.method) is None
14+
15+
16+
def test_proto_call(benchmark):
17+
obj = pyclasses.EmptyClass()
18+
assert benchmark(len, obj) == 0
19+
20+
1121
class EmptyClassPy:
12-
pass
22+
def method(self):
23+
pass
24+
25+
def __len__(self) -> int:
26+
return 0
1327

1428

1529
def test_empty_class_init_py(benchmark):
1630
benchmark(EmptyClassPy)
1731

1832

33+
def test_method_call_py(benchmark):
34+
obj = EmptyClassPy()
35+
assert benchmark(obj.method) == pyclasses.EmptyClass().method()
36+
37+
38+
def test_proto_call_py(benchmark):
39+
obj = EmptyClassPy()
40+
assert benchmark(len, obj) == len(pyclasses.EmptyClass())
41+
42+
1943
def test_iter():
2044
i = pyclasses.PyClassIter()
2145
assert next(i) == 1

0 commit comments

Comments
 (0)