pyo3/benches/bench_decimal.rs
Doug Goldstein 8e0bde14c9 implement Decimal to rust_decimal conversions
Implement conversion between rust_decimal::Decimal and decimal.Decimal
from Python's stdlib. The C API does not appear to be exposed on the
Python side so we need to call into it via Python.
2023-04-22 19:55:21 +02:00

33 lines
779 B
Rust

use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use pyo3::prelude::*;
use pyo3::types::PyDict;
use rust_decimal::Decimal;
fn decimal_via_extract(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let locals = PyDict::new(py);
py.run(
r#"
import decimal
py_dec = decimal.Decimal("0.0")
"#,
None,
Some(locals),
)
.unwrap();
let py_dec = locals.get_item("py_dec").unwrap();
b.iter(|| {
let _: Decimal = black_box(py_dec).extract().unwrap();
});
})
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("decimal_via_extract", decimal_via_extract);
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);