|
1 |
| -use criterion::{criterion_group, criterion_main, Bencher, Criterion}; |
| 1 | +#[cfg(feature = "macros")] |
| 2 | +use criterion::{criterion_group, criterion_main, Criterion}; |
2 | 3 |
|
3 |
| -use pyo3::{class::PyObjectProtocol, prelude::*, type_object::LazyStaticType}; |
| 4 | +#[cfg(feature = "macros")] |
| 5 | +mod m { |
| 6 | + use pyo3::{class::PyObjectProtocol, prelude::*, type_object::LazyStaticType}; |
4 | 7 |
|
5 |
| -/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle. |
6 |
| -#[pyclass] |
7 |
| -struct MyClass { |
8 |
| - #[pyo3(get, set)] |
9 |
| - elements: Vec<i32>, |
10 |
| -} |
11 |
| - |
12 |
| -#[pymethods] |
13 |
| -impl MyClass { |
14 |
| - #[new] |
15 |
| - fn new(elements: Vec<i32>) -> Self { |
16 |
| - Self { elements } |
| 8 | + /// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle. |
| 9 | + #[pyclass] |
| 10 | + struct MyClass { |
| 11 | + #[pyo3(get, set)] |
| 12 | + elements: Vec<i32>, |
17 | 13 | }
|
18 | 14 |
|
19 |
| - fn __call__(&mut self, new_element: i32) -> usize { |
20 |
| - self.elements.push(new_element); |
21 |
| - self.elements.len() |
| 15 | + #[pymethods] |
| 16 | + impl MyClass { |
| 17 | + #[new] |
| 18 | + fn new(elements: Vec<i32>) -> Self { |
| 19 | + Self { elements } |
| 20 | + } |
| 21 | + |
| 22 | + fn __call__(&mut self, new_element: i32) -> usize { |
| 23 | + self.elements.push(new_element); |
| 24 | + self.elements.len() |
| 25 | + } |
22 | 26 | }
|
23 |
| -} |
24 | 27 |
|
25 |
| -#[pyproto] |
26 |
| -impl PyObjectProtocol for MyClass { |
27 |
| - /// A basic __str__ implementation. |
28 |
| - fn __str__(&self) -> &'static str { |
29 |
| - "MyClass" |
| 28 | + #[pyproto] |
| 29 | + impl PyObjectProtocol for MyClass { |
| 30 | + /// A basic __str__ implementation. |
| 31 | + fn __str__(&self) -> &'static str { |
| 32 | + "MyClass" |
| 33 | + } |
30 | 34 | }
|
31 |
| -} |
32 | 35 |
|
33 |
| -fn first_time_init(b: &mut Bencher) { |
34 |
| - let gil = Python::acquire_gil(); |
35 |
| - let py = gil.python(); |
36 |
| - b.iter(|| { |
37 |
| - // This is using an undocumented internal PyO3 API to measure pyclass performance; please |
38 |
| - // don't use this in your own code! |
39 |
| - let ty = LazyStaticType::new(); |
40 |
| - ty.get_or_init::<MyClass>(py); |
41 |
| - }); |
| 36 | + pub fn first_time_init(b: &mut criterion::Bencher) { |
| 37 | + let gil = Python::acquire_gil(); |
| 38 | + let py = gil.python(); |
| 39 | + b.iter(|| { |
| 40 | + // This is using an undocumented internal PyO3 API to measure pyclass performance; please |
| 41 | + // don't use this in your own code! |
| 42 | + let ty = LazyStaticType::new(); |
| 43 | + ty.get_or_init::<MyClass>(py); |
| 44 | + }); |
| 45 | + } |
42 | 46 | }
|
43 | 47 |
|
| 48 | +#[cfg(feature = "macros")] |
44 | 49 | fn criterion_benchmark(c: &mut Criterion) {
|
45 |
| - c.bench_function("first_time_init", first_time_init); |
| 50 | + c.bench_function("first_time_init", m::first_time_init); |
46 | 51 | }
|
47 | 52 |
|
| 53 | +#[cfg(feature = "macros")] |
48 | 54 | criterion_group!(benches, criterion_benchmark);
|
| 55 | + |
| 56 | +#[cfg(feature = "macros")] |
49 | 57 | criterion_main!(benches);
|
| 58 | + |
| 59 | +#[cfg(not(feature = "macros"))] |
| 60 | +fn main() { |
| 61 | + unimplemented!( |
| 62 | + "benchmarking `bench_pyclass` is only available with the `macros` feature enabled" |
| 63 | + ); |
| 64 | +} |
0 commit comments