From ff6fb5dcc2b09150d3f00239b26a34aeb3c5c20f Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Wed, 22 Dec 2021 00:27:13 +0000 Subject: [PATCH] benches: add bench_frompyobject --- Cargo.toml | 4 ++++ benches/bench_frompyobject.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 benches/bench_frompyobject.rs diff --git a/Cargo.toml b/Cargo.toml index b81f1ae6..b68afedb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,6 +91,10 @@ harness = false name = "bench_dict" harness = false +[[bench]] +name = "bench_frompyobject" +harness = false + [[bench]] name = "bench_gil" harness = false diff --git a/benches/bench_frompyobject.rs b/benches/bench_frompyobject.rs new file mode 100644 index 00000000..6a90900d --- /dev/null +++ b/benches/bench_frompyobject.rs @@ -0,0 +1,26 @@ +use criterion::{criterion_group, criterion_main, Bencher, Criterion}; + +use pyo3::{prelude::*, types::PyString}; + +#[derive(FromPyObject)] +enum ManyTypes { + Int(i32), + Bytes(Vec), + String(String) +} + +fn enum_from_pyobject(b: &mut Bencher) { + Python::with_gil(|py| { + let obj = PyString::new(py, "hello world"); + b.iter(|| { + let _: ManyTypes = obj.extract().unwrap(); + }); + }) +} + +fn criterion_benchmark(c: &mut Criterion) { + c.bench_function("enum_from_pyobject", enum_from_pyobject); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches);