pyo3/benches/bench_dict.rs

36 lines
845 B
Rust
Raw Normal View History

2018-11-14 06:40:08 +00:00
#![feature(test)]
2019-02-01 13:01:18 +00:00
2018-11-14 06:40:08 +00:00
extern crate test;
2019-02-01 13:01:18 +00:00
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
2018-11-14 06:40:08 +00:00
use test::Bencher;
#[bench]
fn iter_dict(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
2018-11-14 06:40:08 +00:00
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for (k, _v) in dict.iter() {
let i: u64 = k.extract().unwrap();
sum += i;
}
});
}
2020-05-02 22:17:21 +00:00
#[bench]
fn dict_get_item(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += dict.get_item(i).unwrap().extract::<usize>().unwrap();
}
});
}