use criterion::{criterion_group, criterion_main, Criterion};
use pyo3::{prelude::*, type_object::LazyStaticType};
#[pyclass]
struct MyClass {
#[pyo3(get, set)]
elements: Vec<i32>,
}
#[pymethods]
impl MyClass {
#[new]
fn new(elements: Vec<i32>) -> Self {
Self { elements }
}
fn __call__(&mut self, new_element: i32) -> usize {
self.elements.push(new_element);
self.elements.len()
}
fn __str__(&self) -> &'static str {
"MyClass"
}
}
pub fn first_time_init(b: &mut criterion::Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
b.iter(|| {
let ty = LazyStaticType::new();
ty.get_or_init::<MyClass>(py);
});
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("first_time_init", first_time_init);
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);