2021-05-26 06:25:00 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
|
2020-09-08 21:45:36 +00:00
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::types::PySet;
|
2020-11-26 09:58:14 +00:00
|
|
|
use std::collections::{BTreeSet, HashSet};
|
2020-09-08 21:45:36 +00:00
|
|
|
|
2022-12-04 08:53:10 +00:00
|
|
|
fn set_new(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
|
|
|
// Create Python objects up-front, so that the benchmark doesn't need to include
|
|
|
|
// the cost of allocating LEN Python integers
|
2023-01-27 08:20:03 +00:00
|
|
|
let elements: Vec<PyObject> = (0..LEN).map(|i| i.into_py(py)).collect();
|
2022-12-04 08:53:10 +00:00
|
|
|
b.iter(|| {
|
|
|
|
let pool = unsafe { py.new_pool() };
|
|
|
|
PySet::new(py, &elements).unwrap();
|
|
|
|
drop(pool);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn iter_set(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
|
|
|
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
|
|
|
|
let mut sum = 0;
|
|
|
|
b.iter(|| {
|
|
|
|
for x in set.iter() {
|
|
|
|
let i: u64 = x.extract().unwrap();
|
|
|
|
sum += i;
|
|
|
|
}
|
|
|
|
});
|
2020-09-08 21:45:36 +00:00
|
|
|
});
|
|
|
|
}
|
2020-11-26 09:58:14 +00:00
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn extract_hashset(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
|
|
|
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
|
|
|
|
b.iter(|| HashSet::<u64>::extract(set));
|
|
|
|
});
|
2020-11-26 09:58:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn extract_btreeset(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
|
|
|
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
|
|
|
|
b.iter(|| BTreeSet::<u64>::extract(set));
|
|
|
|
});
|
2020-11-26 09:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "hashbrown")]
|
2022-03-23 07:07:28 +00:00
|
|
|
fn extract_hashbrown_set(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
|
|
|
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
|
|
|
|
b.iter(|| hashbrown::HashSet::<u64>::extract(set));
|
|
|
|
});
|
2020-11-26 09:58:14 +00:00
|
|
|
}
|
2021-05-26 06:25:00 +00:00
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
2022-12-04 08:53:10 +00:00
|
|
|
c.bench_function("set_new", set_new);
|
2021-05-26 06:25:00 +00:00
|
|
|
c.bench_function("iter_set", iter_set);
|
|
|
|
c.bench_function("extract_hashset", extract_hashset);
|
|
|
|
c.bench_function("extract_btreeset", extract_btreeset);
|
|
|
|
|
|
|
|
#[cfg(feature = "hashbrown")]
|
|
|
|
c.bench_function("extract_hashbrown_set", extract_hashbrown_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|