New benchmarks

This commit is contained in:
David Hewitt 2020-05-02 23:17:21 +01:00
parent 17cf97df7d
commit be09aa00aa
4 changed files with 217 additions and 0 deletions

View File

@ -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::<usize>().unwrap();
}
});
}

35
benches/bench_list.rs Normal file
View File

@ -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::<usize>().unwrap();
}
});
}

133
benches/bench_pyclass.rs Normal file
View File

@ -0,0 +1,133 @@
#![feature(test)]
extern crate test;
use pyo3::prelude::*;
use pyo3::PyIterProtocol;
use test::Bencher;
#[pyclass]
pub struct MyClass {
#[pyo3(get, set)]
obj: PyObject,
#[pyo3(get, set)]
x: i32,
values: Vec<usize>,
}
#[pyclass]
pub struct Iter {
inner: std::vec::IntoIter<usize>,
}
#[pyproto]
impl PyIterProtocol for Iter {
fn __next__(mut slf: PyRefMut<'p, Self>) -> PyResult<Option<usize>> {
Ok(slf.inner.next())
}
}
#[pyproto]
impl PyIterProtocol for MyClass {
fn __iter__(slf: PyRef<'p, Self>) -> PyResult<Py<Iter>> {
let iter = Iter {
inner: slf.values.clone().into_iter(),
};
PyCell::new(slf.py(), iter).map(Into::into)
}
}
#[bench]
fn create_many_pyclass(b: &mut Bencher) {
b.iter(|| {
for i in 0..20 {
let gil = Python::acquire_gil();
let py = gil.python();
for j in 0..200 {
let _ = PyCell::new(
py,
MyClass {
obj: py.None(),
x: i * j,
values: vec![],
},
);
}
}
});
}
#[bench]
fn iter_pyclass(b: &mut Bencher) {
let mut sum = 0;
b.iter(|| {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = PyCell::new(
py,
MyClass {
obj: py.None(),
x: 0,
values: (0..10_000).into_iter().collect(),
},
)
.unwrap()
.to_object(py);
for x in obj.as_ref(py).iter().unwrap() {
sum += x.unwrap().extract::<usize>().unwrap();
}
});
}
#[bench]
fn get_property(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = PyCell::new(
py,
MyClass {
obj: py.None(),
x: 1,
values: vec![],
},
)
.unwrap()
.to_object(py);
let any = obj.as_ref(py);
let mut sum = 0;
b.iter(|| {
for _ in 0..4_000 {
sum += any.getattr("x").unwrap().extract::<usize>().unwrap();
}
});
}
#[bench]
fn set_property(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = PyCell::new(
py,
MyClass {
obj: py.None(),
x: 1,
values: vec![],
},
)
.unwrap()
.to_object(py);
let any = obj.as_ref(py);
b.iter(|| {
for _ in 0..4_000 {
any.setattr("obj", py.None()).unwrap();
}
});
}

35
benches/bench_tuple.rs Normal file
View File

@ -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::<usize>().unwrap();
}
});
}