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::PyList;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:26:20 +00:00
|
|
|
fn list_new(b: &mut Bencher) {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
const LEN: usize = 50_000;
|
|
|
|
b.iter(|| PyList::new(py, 0..LEN));
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:17:21 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-05-26 06:25:00 +00:00
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
c.bench_function("iter_list", iter_list);
|
2021-06-06 07:26:20 +00:00
|
|
|
c.bench_function("list_new", list_new);
|
2021-05-26 06:25:00 +00:00
|
|
|
c.bench_function("list_get_item", list_get_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|