Skip to content

Commit

Permalink
add benchmark for class / method calls (#4016)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt authored Mar 30, 2024
1 parent 74d9d23 commit 22e8dd1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pytests/src/pyclasses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ impl EmptyClass {
fn new() -> Self {
EmptyClass {}
}

fn method(&self) {}

fn __len__(&self) -> usize {
0
}
}

/// This is for demonstrating how to return a value from __next__
Expand Down
26 changes: 25 additions & 1 deletion pytests/tests/test_pyclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,38 @@ def test_empty_class_init(benchmark):
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:
pass
def method(self):
pass

def __len__(self) -> int:
return 0


def test_empty_class_init_py(benchmark):
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():
i = pyclasses.PyClassIter()
assert next(i) == 1
Expand Down

0 comments on commit 22e8dd1

Please sign in to comment.