Skip to content

Commit

Permalink
Benchmark WithPosition::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Oct 3, 2023
1 parent 4fd6553 commit 573d406
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions benches/bench1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use itertools::free::cloned;
use itertools::iproduct;
use itertools::Itertools;
use itertools::Position;

use std::cmp;
use std::iter::repeat;
Expand Down Expand Up @@ -800,6 +801,22 @@ fn permutations_slice(c: &mut Criterion) {
});
}

fn with_position_fold(c: &mut Criterion) {
let v = black_box((0..10240).collect_vec());
c.bench_function("with_position fold", move |b| {
b.iter(|| {
v.iter()
.with_position()
.fold(0, |acc, (pos, &item)| match pos {
Position::Middle => acc + item,
Position::First => acc - 2 * item,
Position::Last => acc + 2 * item,
Position::Only => acc + 5 * item,
})
})
});
}

criterion_group!(
benches,
slice_iter,
Expand Down Expand Up @@ -846,5 +863,6 @@ criterion_group!(
permutations_iter,
permutations_range,
permutations_slice,
with_position_fold,
);
criterion_main!(benches);

0 comments on commit 573d406

Please sign in to comment.