Skip to content

Commit 78c51d3

Browse files
authored
Merge pull request #891 from davidhewitt/benches
New benchmarks
2 parents e9bec07 + e153312 commit 78c51d3

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

benches/bench_dict.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ fn iter_dict(b: &mut Bencher) {
1919
}
2020
});
2121
}
22+
23+
#[bench]
24+
fn dict_get_item(b: &mut Bencher) {
25+
let gil = Python::acquire_gil();
26+
let py = gil.python();
27+
const LEN: usize = 50_000;
28+
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
29+
let mut sum = 0;
30+
b.iter(|| {
31+
for i in 0..LEN {
32+
sum += dict.get_item(i).unwrap().extract::<usize>().unwrap();
33+
}
34+
});
35+
}

benches/bench_list.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(test)]
2+
3+
extern crate test;
4+
use pyo3::prelude::*;
5+
use pyo3::types::PyList;
6+
use test::Bencher;
7+
8+
#[bench]
9+
fn iter_list(b: &mut Bencher) {
10+
let gil = Python::acquire_gil();
11+
let py = gil.python();
12+
const LEN: usize = 100_000;
13+
let list = PyList::new(py, 0..LEN);
14+
let mut sum = 0;
15+
b.iter(|| {
16+
for x in list.iter() {
17+
let i: u64 = x.extract().unwrap();
18+
sum += i;
19+
}
20+
});
21+
}
22+
23+
#[bench]
24+
fn list_get_item(b: &mut Bencher) {
25+
let gil = Python::acquire_gil();
26+
let py = gil.python();
27+
const LEN: usize = 50_000;
28+
let list = PyList::new(py, 0..LEN);
29+
let mut sum = 0;
30+
b.iter(|| {
31+
for i in 0..LEN {
32+
sum += list.get_item(i as isize).extract::<usize>().unwrap();
33+
}
34+
});
35+
}

benches/bench_tuple.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(test)]
2+
3+
extern crate test;
4+
use pyo3::prelude::*;
5+
use pyo3::types::PyTuple;
6+
use test::Bencher;
7+
8+
#[bench]
9+
fn iter_tuple(b: &mut Bencher) {
10+
let gil = Python::acquire_gil();
11+
let py = gil.python();
12+
const LEN: usize = 100_000;
13+
let tuple = PyTuple::new(py, 0..LEN);
14+
let mut sum = 0;
15+
b.iter(|| {
16+
for x in tuple.iter() {
17+
let i: u64 = x.extract().unwrap();
18+
sum += i;
19+
}
20+
});
21+
}
22+
23+
#[bench]
24+
fn tuple_get_item(b: &mut Bencher) {
25+
let gil = Python::acquire_gil();
26+
let py = gil.python();
27+
const LEN: usize = 50_000;
28+
let tuple = PyTuple::new(py, 0..LEN);
29+
let mut sum = 0;
30+
b.iter(|| {
31+
for i in 0..LEN {
32+
sum += tuple.get_item(i).extract::<usize>().unwrap();
33+
}
34+
});
35+
}

0 commit comments

Comments
 (0)