2024-02-22 09:35:47 +00:00
|
|
|
use std::hint::black_box;
|
|
|
|
|
2023-08-29 06:26:36 +00:00
|
|
|
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
|
2020-11-19 14:27:04 +00:00
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
|
|
|
|
macro_rules! test_module {
|
|
|
|
($py:ident, $code:literal) => {
|
2024-02-22 09:35:47 +00:00
|
|
|
PyModule::from_code_bound($py, $code, file!(), "test_module")
|
|
|
|
.expect("module creation failed")
|
2020-11-19 14:27:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bench_call_0(b: &mut Bencher<'_>) {
|
2020-11-19 14:27:04 +00:00
|
|
|
Python::with_gil(|py| {
|
2021-12-09 00:03:50 +00:00
|
|
|
let module = test_module!(py, "def foo(): pass");
|
2020-11-19 14:27:04 +00:00
|
|
|
|
2024-02-22 09:35:47 +00:00
|
|
|
let foo_module = &module.getattr("foo").unwrap();
|
2020-11-19 14:27:04 +00:00
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
for _ in 0..1000 {
|
2024-02-22 09:35:47 +00:00
|
|
|
black_box(foo_module).call0().unwrap();
|
2020-11-19 14:27:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bench_call_method_0(b: &mut Bencher<'_>) {
|
2020-11-19 14:27:04 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let module = test_module!(
|
|
|
|
py,
|
2021-12-09 00:03:50 +00:00
|
|
|
"
|
|
|
|
class Foo:
|
|
|
|
def foo(self):
|
|
|
|
pass
|
|
|
|
"
|
2020-11-19 14:27:04 +00:00
|
|
|
);
|
|
|
|
|
2024-02-22 09:35:47 +00:00
|
|
|
let foo_module = &module.getattr("Foo").unwrap().call0().unwrap();
|
2020-11-19 14:27:04 +00:00
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
for _ in 0..1000 {
|
2024-02-22 09:35:47 +00:00
|
|
|
black_box(foo_module).call_method0("foo").unwrap();
|
2020-11-19 14:27:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2021-05-26 06:25:00 +00:00
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
c.bench_function("call_0", bench_call_0);
|
|
|
|
c.bench_function("call_method_0", bench_call_method_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|