2021-05-26 06:25:00 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
|
2020-05-02 22:17:21 +00:00
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::types::PyTuple;
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn iter_tuple(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2020-05-02 22:17:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn tuple_new(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
|
|
|
b.iter(|| PyTuple::new(py, 0..LEN));
|
|
|
|
});
|
2021-06-05 07:01:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn tuple_get_item(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
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).unwrap().extract::<usize>().unwrap();
|
|
|
|
}
|
|
|
|
});
|
2021-07-22 17:22:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-30 08:13:43 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
2022-03-23 07:07:28 +00:00
|
|
|
fn tuple_get_item_unchecked(b: &mut Bencher<'_>) {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
|
|
|
let tuple = PyTuple::new(py, 0..LEN);
|
|
|
|
let mut sum = 0;
|
|
|
|
b.iter(|| {
|
|
|
|
for i in 0..LEN {
|
|
|
|
unsafe {
|
|
|
|
sum += tuple.get_item_unchecked(i).extract::<usize>().unwrap();
|
|
|
|
}
|
2021-07-22 17:22:38 +00:00
|
|
|
}
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2020-05-02 22:17:21 +00:00
|
|
|
});
|
|
|
|
}
|
2021-05-26 06:25:00 +00:00
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
c.bench_function("iter_tuple", iter_tuple);
|
2021-06-05 07:01:57 +00:00
|
|
|
c.bench_function("tuple_new", tuple_new);
|
2021-05-26 06:25:00 +00:00
|
|
|
c.bench_function("tuple_get_item", tuple_get_item);
|
2021-08-30 08:13:43 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
2021-07-22 17:22:38 +00:00
|
|
|
c.bench_function("tuple_get_item_unchecked", tuple_get_item_unchecked);
|
2021-05-26 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|