Skip to content

Commit 9b28180

Browse files
Philippe-Choletphimuemue
authored andcommitted
Benchmark WithPosition::fold
1 parent 90b26c1 commit 9b28180

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
@@ -1,6 +1,7 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
22
use itertools::free::cloned;
33
use itertools::Itertools;
4+
use itertools::Position;
45
use itertools::{iproduct, EitherOrBoth};
56

67
use std::cmp;
@@ -819,6 +820,22 @@ fn permutations_slice(c: &mut Criterion) {
819820
});
820821
}
821822

823+
fn with_position_fold(c: &mut Criterion) {
824+
let v = black_box((0..10240).collect_vec());
825+
c.bench_function("with_position fold", move |b| {
826+
b.iter(|| {
827+
v.iter()
828+
.with_position()
829+
.fold(0, |acc, (pos, &item)| match pos {
830+
Position::Middle => acc + item,
831+
Position::First => acc - 2 * item,
832+
Position::Last => acc + 2 * item,
833+
Position::Only => acc + 5 * item,
834+
})
835+
})
836+
});
837+
}
838+
822839
criterion_group!(
823840
benches,
824841
slice_iter,
@@ -866,5 +883,6 @@ criterion_group!(
866883
permutations_iter,
867884
permutations_range,
868885
permutations_slice,
886+
with_position_fold,
869887
);
870888
criterion_main!(benches);

0 commit comments

Comments
 (0)