Skip to content

Commit

Permalink
benches: add map / set extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Nov 26, 2020
1 parent 47a731b commit 02b90c4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions benches/bench_dict.rs
Expand Up @@ -3,6 +3,7 @@
extern crate test;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use std::collections::{BTreeMap, HashMap};
use test::Bencher;

#[bench]
Expand Down Expand Up @@ -33,3 +34,31 @@ fn dict_get_item(b: &mut Bencher) {
}
});
}

#[bench]
fn extract_hashmap(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| HashMap::<u64, u64>::extract(dict));
}

#[bench]
fn extract_btreemap(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| BTreeMap::<u64, u64>::extract(dict));
}

#[bench]
#[cfg(feature = "hashbrown")]
fn extract_hashbrown_map(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| hashbrown::HashMap::<u64, u64>::extract(dict));
}
29 changes: 29 additions & 0 deletions benches/bench_set.rs
Expand Up @@ -3,6 +3,7 @@
extern crate test;
use pyo3::prelude::*;
use pyo3::types::PySet;
use std::collections::{BTreeSet, HashSet};
use test::Bencher;

#[bench]
Expand All @@ -19,3 +20,31 @@ fn iter_set(b: &mut Bencher) {
}
});
}

#[bench]
fn extract_hashset(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| HashSet::<u64>::extract(set));
}

#[bench]
fn extract_btreeset(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| BTreeSet::<u64>::extract(set));
}

#[bench]
#[cfg(feature = "hashbrown")]
fn extract_hashbrown_set(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| hashbrown::HashSet::<u64>::extract(set));
}

0 comments on commit 02b90c4

Please sign in to comment.