add benchmark for class / method calls (#4016)

This commit is contained in:
David Hewitt 2024-03-30 20:29:39 +00:00 committed by GitHub
parent 74d9d23ba0
commit 22e8dd10e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View File

@ -11,6 +11,12 @@ impl EmptyClass {
fn new() -> Self { fn new() -> Self {
EmptyClass {} EmptyClass {}
} }
fn method(&self) {}
fn __len__(&self) -> usize {
0
}
} }
/// This is for demonstrating how to return a value from __next__ /// This is for demonstrating how to return a value from __next__

View File

@ -8,14 +8,38 @@ def test_empty_class_init(benchmark):
benchmark(pyclasses.EmptyClass) benchmark(pyclasses.EmptyClass)
def test_method_call(benchmark):
obj = pyclasses.EmptyClass()
assert benchmark(obj.method) is None
def test_proto_call(benchmark):
obj = pyclasses.EmptyClass()
assert benchmark(len, obj) == 0
class EmptyClassPy: class EmptyClassPy:
def method(self):
pass pass
def __len__(self) -> int:
return 0
def test_empty_class_init_py(benchmark): def test_empty_class_init_py(benchmark):
benchmark(EmptyClassPy) benchmark(EmptyClassPy)
def test_method_call_py(benchmark):
obj = EmptyClassPy()
assert benchmark(obj.method) == pyclasses.EmptyClass().method()
def test_proto_call_py(benchmark):
obj = EmptyClassPy()
assert benchmark(len, obj) == len(pyclasses.EmptyClass())
def test_iter(): def test_iter():
i = pyclasses.PyClassIter() i = pyclasses.PyClassIter()
assert next(i) == 1 assert next(i) == 1