Skip to content

Commit

Permalink
add benchmark for mapping conversion from dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 15, 2023
1 parent 40709db commit 59630be
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion benches/bench_dict.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use pyo3::{prelude::*, types::PyMapping};
use std::collections::{BTreeMap, HashMap};

fn iter_dict(b: &mut Bencher<'_>) {
Expand Down Expand Up @@ -63,6 +63,19 @@ fn extract_hashbrown_map(b: &mut Bencher<'_>) {
});
}

fn mapping_from_dict(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64)
.map(|i| (i, i * 2))
.into_py_dict(py)
.to_object(py);
b.iter(|| {
let _: &PyMapping = dict.extract(py).unwrap();
});
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("iter_dict", iter_dict);
c.bench_function("dict_new", dict_new);
Expand All @@ -72,6 +85,8 @@ fn criterion_benchmark(c: &mut Criterion) {

#[cfg(feature = "hashbrown")]
c.bench_function("extract_hashbrown_map", extract_hashbrown_map);

c.bench_function("mapping_from_dict", mapping_from_dict);
}

criterion_group!(benches, criterion_benchmark);
Expand Down

0 comments on commit 59630be

Please sign in to comment.