diff --git a/pyo3-benches/benches/bench_pyclass.rs b/pyo3-benches/benches/bench_pyclass.rs index b917a4acc08..985d9217b9e 100644 --- a/pyo3-benches/benches/bench_pyclass.rs +++ b/pyo3-benches/benches/bench_pyclass.rs @@ -1,5 +1,5 @@ use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; -use pyo3::{impl_::pyclass::LazyTypeObject, prelude::*}; +use pyo3::{impl_::pyclass::LazyTypeObject, intern, prelude::*}; /// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle. #[pyclass] @@ -15,6 +15,12 @@ impl MyClass { Self { elements } } + fn method_call(&self) {} + + fn __len__(&self) -> usize { + self.elements.len() + } + fn __call__(&mut self, new_element: i32) -> usize { self.elements.push(new_element); self.elements.len() @@ -37,8 +43,24 @@ pub fn first_time_init(b: &mut Bencher<'_>) { }); } +pub fn method_call(b: &mut Bencher<'_>) { + Python::with_gil(|py| { + let obj = Bound::new(py, MyClass::new(vec![])).unwrap(); + b.iter(|| obj.call_method0(intern!(py, "method_call"))); + }); +} + +pub fn proto_call(b: &mut Bencher<'_>) { + Python::with_gil(|py| { + let obj = Bound::new(py, MyClass::new(vec![])).unwrap(); + b.iter(|| obj.len()); + }); +} + fn criterion_benchmark(c: &mut Criterion) { c.bench_function("first_time_init", first_time_init); + c.bench_function("method_call", method_call); + c.bench_function("proto_call", proto_call); } criterion_group!(benches, criterion_benchmark);