2023-08-29 06:26:36 +00:00
|
|
|
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
|
2019-02-01 13:01:18 +00:00
|
|
|
|
|
|
|
use pyo3::types::IntoPyDict;
|
2023-02-15 08:07:07 +00:00
|
|
|
use pyo3::{prelude::*, types::PyMapping};
|
2020-11-26 09:58:14 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2023-11-29 11:10:36 +00:00
|
|
|
use std::hint::black_box;
|
2018-11-14 06:40:08 +00:00
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn iter_dict(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
2024-02-11 23:55:56 +00:00
|
|
|
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
|
2022-07-19 17:34:23 +00:00
|
|
|
let mut sum = 0;
|
|
|
|
b.iter(|| {
|
2024-02-11 23:55:56 +00:00
|
|
|
for (k, _v) in dict.iter() {
|
2022-07-19 17:34:23 +00:00
|
|
|
let i: u64 = k.extract().unwrap();
|
|
|
|
sum += i;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
2018-11-14 06:40:08 +00:00
|
|
|
}
|
2020-05-02 22:17:21 +00:00
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn dict_new(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
2024-02-11 23:55:56 +00:00
|
|
|
b.iter_with_large_drop(|| (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py));
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2021-06-06 07:26:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn dict_get_item(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
2024-02-11 23:55:56 +00:00
|
|
|
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
|
2022-07-19 17:34:23 +00:00
|
|
|
let mut sum = 0;
|
|
|
|
b.iter(|| {
|
|
|
|
for i in 0..LEN {
|
2023-07-19 20:24:24 +00:00
|
|
|
sum += dict
|
|
|
|
.get_item(i)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.extract::<usize>()
|
|
|
|
.unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
}
|
|
|
|
});
|
2020-05-02 22:17:21 +00:00
|
|
|
});
|
|
|
|
}
|
2020-11-26 09:58:14 +00:00
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn extract_hashmap(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
2024-02-11 23:55:56 +00:00
|
|
|
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
|
|
|
|
b.iter(|| HashMap::<u64, u64>::extract_bound(&dict));
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2020-11-26 09:58:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn extract_btreemap(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
2024-02-11 23:55:56 +00:00
|
|
|
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
|
|
|
|
b.iter(|| BTreeMap::<u64, u64>::extract_bound(&dict));
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2020-11-26 09:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "hashbrown")]
|
2022-03-23 07:07:28 +00:00
|
|
|
fn extract_hashbrown_map(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
2024-02-11 23:55:56 +00:00
|
|
|
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
|
|
|
|
b.iter(|| hashbrown::HashMap::<u64, u64>::extract_bound(&dict));
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2020-11-26 09:58:14 +00:00
|
|
|
}
|
2021-05-26 06:25:00 +00:00
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-02-15 08:07:07 +00:00
|
|
|
fn mapping_from_dict(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 100_000;
|
2024-02-11 23:55:56 +00:00
|
|
|
let dict = &(0..LEN as u64).map(|i| (i, i * 2)).into_py_dict_bound(py);
|
2023-11-29 11:10:36 +00:00
|
|
|
b.iter(|| black_box(dict).downcast::<PyMapping>().unwrap());
|
2023-02-15 08:07:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-26 06:25:00 +00:00
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
c.bench_function("iter_dict", iter_dict);
|
2021-06-06 07:26:20 +00:00
|
|
|
c.bench_function("dict_new", dict_new);
|
2021-05-26 06:25:00 +00:00
|
|
|
c.bench_function("dict_get_item", dict_get_item);
|
|
|
|
c.bench_function("extract_hashmap", extract_hashmap);
|
|
|
|
c.bench_function("extract_btreemap", extract_btreemap);
|
|
|
|
|
|
|
|
#[cfg(feature = "hashbrown")]
|
|
|
|
c.bench_function("extract_hashbrown_map", extract_hashbrown_map);
|
2023-02-15 08:07:07 +00:00
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-02-15 08:07:07 +00:00
|
|
|
c.bench_function("mapping_from_dict", mapping_from_dict);
|
2021-05-26 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|