pyo3/benches/bench_call.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2021-05-26 06:25:00 +00:00
use criterion::{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) => {
2021-12-09 00:03:50 +00:00
PyModule::from_code($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
2021-07-30 22:30:56 +00:00
let foo_module = module.getattr("foo").unwrap();
2020-11-19 14:27:04 +00:00
b.iter(|| {
for _ in 0..1000 {
2021-07-30 22:30:56 +00:00
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
);
2021-07-30 22:30:56 +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 {
2021-07-30 22:30:56 +00:00
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);