Skip to content

Commit 20611c8

Browse files
Benchmark WithPosition::fold
1 parent 00c31ac commit 20611c8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

benches/bench1.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
22
use itertools::free::cloned;
33
use itertools::iproduct;
44
use itertools::Itertools;
5+
use itertools::Position;
56

67
use std::cmp;
78
use std::iter::repeat;
@@ -800,6 +801,22 @@ fn permutations_slice(c: &mut Criterion) {
800801
});
801802
}
802803

804+
fn with_position_fold(c: &mut Criterion) {
805+
let v = black_box((0..10240).collect_vec());
806+
c.bench_function("with_position fold", move |b| {
807+
b.iter(|| {
808+
v.iter()
809+
.with_position()
810+
.fold(0, |acc, (pos, &item)| match pos {
811+
Position::Middle => acc + item,
812+
Position::First => acc - 2 * item,
813+
Position::Last => acc + 2 * item,
814+
Position::Only => acc + 5 * item,
815+
})
816+
})
817+
});
818+
}
819+
803820
criterion_group!(
804821
benches,
805822
slice_iter,
@@ -846,5 +863,6 @@ criterion_group!(
846863
permutations_iter,
847864
permutations_range,
848865
permutations_slice,
866+
with_position_fold,
849867
);
850868
criterion_main!(benches);

0 commit comments

Comments
 (0)