Skip to content

Commit

Permalink
add benchmarks for sequence conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 13, 2023
1 parent 6116e37 commit b64200e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 12 additions & 1 deletion benches/bench_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::prelude::*;
use pyo3::types::PyList;
use pyo3::types::{PyList, PySequence};

fn iter_list(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
Expand Down Expand Up @@ -53,12 +53,23 @@ fn list_get_item_unchecked(b: &mut Bencher<'_>) {
});
}

fn sequence_from_list(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let list = PyList::new(py, 0..LEN).to_object(py);
b.iter(|| {
let _: &PySequence = list.extract(py).unwrap();
});
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("iter_list", iter_list);
c.bench_function("list_new", list_new);
c.bench_function("list_get_item", list_get_item);
#[cfg(not(Py_LIMITED_API))]
c.bench_function("list_get_item_unchecked", list_get_item_unchecked);
c.bench_function("sequence_from_list", sequence_from_list);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
11 changes: 10 additions & 1 deletion benches/bench_tuple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::prelude::*;
use pyo3::types::PyTuple;
use pyo3::types::{PySequence, PyTuple};

fn iter_tuple(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
Expand Down Expand Up @@ -53,12 +53,21 @@ fn tuple_get_item_unchecked(b: &mut Bencher<'_>) {
});
}

fn sequence_from_tuple(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let tuple = PyTuple::new(py, 0..LEN).to_object(py);
b.iter(|| tuple.extract::<&PySequence>(py).unwrap());
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("iter_tuple", iter_tuple);
c.bench_function("tuple_new", tuple_new);
c.bench_function("tuple_get_item", tuple_get_item);
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
c.bench_function("tuple_get_item_unchecked", tuple_get_item_unchecked);
c.bench_function("sequence_from_tuple", sequence_from_tuple);
}

criterion_group!(benches, criterion_benchmark);
Expand Down

0 comments on commit b64200e

Please sign in to comment.