Skip to content

Commit

Permalink
benchmark PySet::new
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 4, 2022
1 parent 55592af commit 65ead64
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions benches/bench_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ use pyo3::prelude::*;
use pyo3::types::PySet;
use std::collections::{BTreeSet, HashSet};

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
let elements: Vec<PyObject> = (0..LEN).into_iter().map(|i| i.into_py(py)).collect();
b.iter(|| {
let pool = unsafe { py.new_pool() };
PySet::new(py, &elements).unwrap();
drop(pool);
});
});
}

fn iter_set(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
Expand Down Expand Up @@ -44,6 +58,7 @@ fn extract_hashbrown_set(b: &mut Bencher<'_>) {
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("set_new", set_new);
c.bench_function("iter_set", iter_set);
c.bench_function("extract_hashset", extract_hashset);
c.bench_function("extract_btreeset", extract_btreeset);
Expand Down

0 comments on commit 65ead64

Please sign in to comment.