diff --git a/benches/bench_dict.rs b/benches/bench_dict.rs index 73ed49fa42e..c82fb7dc223 100644 --- a/benches/bench_dict.rs +++ b/benches/bench_dict.rs @@ -19,3 +19,17 @@ fn iter_dict(b: &mut Bencher) { } }); } + +#[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::().unwrap(); + } + }); +} diff --git a/benches/bench_list.rs b/benches/bench_list.rs new file mode 100644 index 00000000000..48c5f5b621e --- /dev/null +++ b/benches/bench_list.rs @@ -0,0 +1,35 @@ +#![feature(test)] + +extern crate test; +use pyo3::prelude::*; +use pyo3::types::PyList; +use test::Bencher; + +#[bench] +fn iter_list(b: &mut Bencher) { + let gil = Python::acquire_gil(); + let py = gil.python(); + const LEN: usize = 100_000; + let list = PyList::new(py, 0..LEN); + let mut sum = 0; + b.iter(|| { + for x in list.iter() { + let i: u64 = x.extract().unwrap(); + sum += i; + } + }); +} + +#[bench] +fn list_get_item(b: &mut Bencher) { + let gil = Python::acquire_gil(); + let py = gil.python(); + const LEN: usize = 50_000; + let list = PyList::new(py, 0..LEN); + let mut sum = 0; + b.iter(|| { + for i in 0..LEN { + sum += list.get_item(i as isize).extract::().unwrap(); + } + }); +} diff --git a/benches/bench_tuple.rs b/benches/bench_tuple.rs new file mode 100644 index 00000000000..a8db6e3f30c --- /dev/null +++ b/benches/bench_tuple.rs @@ -0,0 +1,35 @@ +#![feature(test)] + +extern crate test; +use pyo3::prelude::*; +use pyo3::types::PyTuple; +use test::Bencher; + +#[bench] +fn iter_tuple(b: &mut Bencher) { + let gil = Python::acquire_gil(); + let py = gil.python(); + const LEN: usize = 100_000; + let tuple = PyTuple::new(py, 0..LEN); + let mut sum = 0; + b.iter(|| { + for x in tuple.iter() { + let i: u64 = x.extract().unwrap(); + sum += i; + } + }); +} + +#[bench] +fn tuple_get_item(b: &mut Bencher) { + let gil = Python::acquire_gil(); + let py = gil.python(); + const LEN: usize = 50_000; + let tuple = PyTuple::new(py, 0..LEN); + let mut sum = 0; + b.iter(|| { + for i in 0..LEN { + sum += tuple.get_item(i).extract::().unwrap(); + } + }); +}