2021-12-09 00:03:50 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
2023-02-11 20:51:52 +00:00
|
|
|
use pyo3::{impl_::pyclass::LazyTypeObject, prelude::*};
|
2021-02-13 08:37:56 +00:00
|
|
|
|
2021-12-22 00:29:22 +00:00
|
|
|
/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle.
|
|
|
|
#[pyclass]
|
|
|
|
struct MyClass {
|
|
|
|
#[pyo3(get, set)]
|
|
|
|
elements: Vec<i32>,
|
|
|
|
}
|
2021-02-13 08:37:56 +00:00
|
|
|
|
2021-12-22 00:29:22 +00:00
|
|
|
#[pymethods]
|
|
|
|
impl MyClass {
|
|
|
|
#[new]
|
|
|
|
fn new(elements: Vec<i32>) -> Self {
|
|
|
|
Self { elements }
|
2021-02-13 08:37:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 00:29:22 +00:00
|
|
|
fn __call__(&mut self, new_element: i32) -> usize {
|
|
|
|
self.elements.push(new_element);
|
|
|
|
self.elements.len()
|
2021-02-13 08:37:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 00:29:22 +00:00
|
|
|
/// A basic __str__ implementation.
|
|
|
|
fn __str__(&self) -> &'static str {
|
|
|
|
"MyClass"
|
2021-02-13 08:37:56 +00:00
|
|
|
}
|
2021-12-22 00:29:22 +00:00
|
|
|
}
|
2021-02-13 08:37:56 +00:00
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
pub fn first_time_init(b: &mut criterion::Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
b.iter(|| {
|
|
|
|
// This is using an undocumented internal PyO3 API to measure pyclass performance; please
|
|
|
|
// don't use this in your own code!
|
2023-02-11 20:51:52 +00:00
|
|
|
let ty = LazyTypeObject::<MyClass>::new();
|
|
|
|
ty.get_or_init(py);
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2021-12-22 00:29:22 +00:00
|
|
|
});
|
2021-02-13 08:37:56 +00:00
|
|
|
}
|
2021-05-26 06:25:00 +00:00
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
2021-12-22 00:29:22 +00:00
|
|
|
c.bench_function("first_time_init", first_time_init);
|
2021-05-26 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
2021-12-09 00:03:50 +00:00
|
|
|
|
2021-05-26 06:25:00 +00:00
|
|
|
criterion_main!(benches);
|