Skip to content

Commit 8c9ad3a

Browse files
add bench for method call overhead (#6102)
1 parent 89c00d6 commit 8c9ad3a

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

pyo3-benches/benches/bench_pyclass.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ impl MyClass {
2626
fn __str__(&self) -> &'static str {
2727
"MyClass"
2828
}
29+
30+
/// Method with low overhead to benchmark method call performance.
31+
fn fast<'py>(self_: PyRef<'py, Self>) -> PyResult<Bound<'py, PyAny>> {
32+
let py = self_.py();
33+
Ok(py.None().bind(py).clone())
34+
}
2935
}
3036

3137
pub fn first_time_init(b: &mut Bencher<'_>) {
@@ -74,6 +80,12 @@ pub fn bench_pyclass(c: &mut Criterion) {
7480
b.iter(|| bound.str());
7581
});
7682
});
83+
c.bench_function("bench_fast", |b| {
84+
Python::attach(|py| {
85+
let bound = Bound::new(py, MyClass::new(vec![1, 2, 3])).unwrap();
86+
b.iter(|| bound.call_method0("fast"));
87+
});
88+
});
7789
}
7890

7991
fn bench_first_time_init(c: &mut Criterion) {

0 commit comments

Comments
 (0)