2024-03-23 20:17:33 +00:00
|
|
|
use std::hint::black_box;
|
|
|
|
|
2023-08-29 06:26:36 +00:00
|
|
|
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
|
2020-05-02 22:17:21 +00:00
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
2023-03-12 18:13:08 +00:00
|
|
|
use pyo3::types::{PyList, PySequence, PyTuple};
|
2020-05-02 22:17:21 +00:00
|
|
|
|
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;
|
2024-01-23 08:44:47 +00:00
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN);
|
2022-07-19 17:34:23 +00:00
|
|
|
let mut sum = 0;
|
|
|
|
b.iter(|| {
|
2024-01-23 08:44:47 +00:00
|
|
|
for x in tuple.iter_borrowed() {
|
2022-07-19 17:34:23 +00:00
|
|
|
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;
|
2024-01-23 08:44:47 +00:00
|
|
|
b.iter_with_large_drop(|| PyTuple::new_bound(py, 0..LEN));
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
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;
|
2024-01-23 08:44:47 +00:00
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN);
|
2022-07-19 17:34:23 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-24 19:17:25 +00:00
|
|
|
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
|
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;
|
2024-01-23 08:44:47 +00:00
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN);
|
2022-07-19 17:34:23 +00:00
|
|
|
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
|
|
|
|
2024-01-23 09:59:37 +00:00
|
|
|
fn tuple_get_borrowed_item(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN);
|
|
|
|
let mut sum = 0;
|
|
|
|
b.iter(|| {
|
|
|
|
for i in 0..LEN {
|
|
|
|
sum += tuple
|
|
|
|
.get_borrowed_item(i)
|
|
|
|
.unwrap()
|
|
|
|
.extract::<usize>()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
|
|
|
|
fn tuple_get_borrowed_item_unchecked(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN);
|
|
|
|
let mut sum = 0;
|
|
|
|
b.iter(|| {
|
|
|
|
for i in 0..LEN {
|
|
|
|
unsafe {
|
|
|
|
sum += tuple
|
|
|
|
.get_borrowed_item_unchecked(i)
|
|
|
|
.extract::<usize>()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-13 21:40:36 +00:00
|
|
|
fn sequence_from_tuple(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
2024-03-23 20:17:33 +00:00
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN).into_any();
|
|
|
|
b.iter(|| black_box(&tuple).downcast::<PySequence>().unwrap());
|
2023-02-13 21:40:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-12 18:13:08 +00:00
|
|
|
fn tuple_new_list(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
2024-01-23 08:44:47 +00:00
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN);
|
|
|
|
b.iter_with_large_drop(|| PyList::new_bound(py, tuple.iter_borrowed()));
|
2023-03-12 18:13:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tuple_to_list(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
const LEN: usize = 50_000;
|
2024-01-23 08:44:47 +00:00
|
|
|
let tuple = PyTuple::new_bound(py, 0..LEN);
|
|
|
|
b.iter_with_large_drop(|| tuple.to_list());
|
2023-03-12 18:13:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:50:04 +00:00
|
|
|
fn tuple_into_py(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
b.iter(|| -> PyObject { (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).into_py(py) });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
2022-12-24 19:17:25 +00:00
|
|
|
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
|
2021-07-22 17:22:38 +00:00
|
|
|
c.bench_function("tuple_get_item_unchecked", tuple_get_item_unchecked);
|
2024-01-23 09:59:37 +00:00
|
|
|
c.bench_function("tuple_get_borrowed_item", tuple_get_borrowed_item);
|
|
|
|
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
|
|
|
|
c.bench_function(
|
|
|
|
"tuple_get_borrowed_item_unchecked",
|
|
|
|
tuple_get_borrowed_item_unchecked,
|
|
|
|
);
|
2023-02-13 21:40:36 +00:00
|
|
|
c.bench_function("sequence_from_tuple", sequence_from_tuple);
|
2023-03-12 18:13:08 +00:00
|
|
|
c.bench_function("tuple_new_list", tuple_new_list);
|
|
|
|
c.bench_function("tuple_to_list", tuple_to_list);
|
2023-07-04 20:50:04 +00:00
|
|
|
c.bench_function("tuple_into_py", tuple_into_py);
|
2021-05-26 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|