Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions pyo3-benches/benches/bench_pyclass.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
use pyo3::conversion::IntoPyObjectExt;
use pyo3::types::PyInt;
use pyo3::{impl_::pyclass::LazyTypeObject, prelude::*};

/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle.
Expand Down Expand Up @@ -37,10 +39,47 @@ pub fn first_time_init(b: &mut Bencher<'_>) {
});
}

fn criterion_benchmark(c: &mut Criterion) {
pub fn bench_pyclass(c: &mut Criterion) {
c.bench_function("bench_pyclass_create", |b| {
Python::attach(|py| {
b.iter_batched(
|| vec![1, 2, 3],
|elements| {
MyClass::new(elements).into_py_any(py).unwrap();
},
BatchSize::SmallInput,
);
});
});
c.bench_function("bench_call", |b| {
Python::attach(|py| {
b.iter_batched(
|| {
(
MyClass::new(vec![1, 2, 3]).into_py_any(py).unwrap(),
PyInt::new(py, 4),
)
},
|(inst, arg)| {
inst.call1(py, (arg,)).unwrap();
},
BatchSize::SmallInput,
);
});
});
c.bench_function("bench_str", |b| {
Python::attach(|py| {
let inst = MyClass::new(vec![1, 2, 3]).into_py_any(py).unwrap();
let bound = inst.bind(py);
b.iter(|| bound.str());
});
});
}

fn bench_first_time_init(c: &mut Criterion) {
c.bench_function("first_time_init", first_time_init);
}

criterion_group!(benches, criterion_benchmark);
criterion_group!(benches, bench_first_time_init, bench_pyclass);

criterion_main!(benches);
Loading