Skip to content

Commit 55c97ce

Browse files
Move fold specializations away from "bench1"
1 parent 92aa4cf commit 55c97ce

File tree

2 files changed

+34
-105
lines changed

2 files changed

+34
-105
lines changed

benches/bench1.rs

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
22
use itertools::free::cloned;
3+
use itertools::iproduct;
34
use itertools::Itertools;
4-
use itertools::Position;
5-
use itertools::{iproduct, EitherOrBoth};
65

76
use std::cmp;
87
use std::iter::repeat;
@@ -391,25 +390,6 @@ fn zip_unchecked_counted_loop3(c: &mut Criterion) {
391390
});
392391
}
393392

394-
fn ziplongest(c: &mut Criterion) {
395-
let v1 = black_box((0..768).collect_vec());
396-
let v2 = black_box((0..1024).collect_vec());
397-
c.bench_function("ziplongest", move |b| {
398-
b.iter(|| {
399-
let zip = v1.iter().zip_longest(v2.iter());
400-
let sum = zip.fold(0u32, |mut acc, val| {
401-
match val {
402-
EitherOrBoth::Both(x, y) => acc += x * y,
403-
EitherOrBoth::Left(x) => acc += x,
404-
EitherOrBoth::Right(y) => acc += y,
405-
}
406-
acc
407-
});
408-
sum
409-
})
410-
});
411-
}
412-
413393
fn group_by_lazy_1(c: &mut Criterion) {
414394
let mut data = vec![0; 1024];
415395
for (index, elt) in data.iter_mut().enumerate() {
@@ -448,25 +428,6 @@ fn group_by_lazy_2(c: &mut Criterion) {
448428
});
449429
}
450430

451-
fn while_some(c: &mut Criterion) {
452-
c.bench_function("while_some", |b| {
453-
b.iter(|| {
454-
let data = black_box(
455-
(0..)
456-
.fuse()
457-
.map(|i| std::char::from_digit(i, 16))
458-
.while_some(),
459-
);
460-
// let result: String = data.fold(String::new(), |acc, ch| acc + &ch.to_string());
461-
let result = data.fold(String::new(), |mut acc, ch| {
462-
acc.push(ch);
463-
acc
464-
});
465-
assert_eq!(result.as_str(), "0123456789abcdef");
466-
});
467-
});
468-
}
469-
470431
fn slice_chunks(c: &mut Criterion) {
471432
let data = vec![0; 1024];
472433

@@ -703,22 +664,6 @@ fn cartesian_product_iterator(c: &mut Criterion) {
703664
});
704665
}
705666

706-
fn cartesian_product_fold(c: &mut Criterion) {
707-
let xs = vec![0; 16];
708-
709-
c.bench_function("cartesian product fold", move |b| {
710-
b.iter(|| {
711-
let mut sum = 0;
712-
iproduct!(&xs, &xs, &xs).fold((), |(), (&x, &y, &z)| {
713-
sum += x;
714-
sum += y;
715-
sum += z;
716-
});
717-
sum
718-
})
719-
});
720-
}
721-
722667
fn multi_cartesian_product_iterator(c: &mut Criterion) {
723668
let xs = [vec![0; 16], vec![0; 16], vec![0; 16]];
724669

@@ -735,22 +680,6 @@ fn multi_cartesian_product_iterator(c: &mut Criterion) {
735680
});
736681
}
737682

738-
fn multi_cartesian_product_fold(c: &mut Criterion) {
739-
let xs = [vec![0; 16], vec![0; 16], vec![0; 16]];
740-
741-
c.bench_function("multi cartesian product fold", move |b| {
742-
b.iter(|| {
743-
let mut sum = 0;
744-
xs.iter().multi_cartesian_product().fold((), |(), x| {
745-
sum += x[0];
746-
sum += x[1];
747-
sum += x[2];
748-
});
749-
sum
750-
})
751-
});
752-
}
753-
754683
fn cartesian_product_nested_for(c: &mut Criterion) {
755684
let xs = vec![0; 16];
756685

@@ -839,33 +768,6 @@ fn permutations_slice(c: &mut Criterion) {
839768
});
840769
}
841770

842-
fn with_position_fold(c: &mut Criterion) {
843-
let v = black_box((0..10240).collect_vec());
844-
c.bench_function("with_position fold", move |b| {
845-
b.iter(|| {
846-
v.iter()
847-
.with_position()
848-
.fold(0, |acc, (pos, &item)| match pos {
849-
Position::Middle => acc + item,
850-
Position::First => acc - 2 * item,
851-
Position::Last => acc + 2 * item,
852-
Position::Only => acc + 5 * item,
853-
})
854-
})
855-
});
856-
}
857-
858-
fn tuple_combinations_fold(c: &mut Criterion) {
859-
let v = black_box((0..64).collect_vec());
860-
c.bench_function("tuple_combinations fold", move |b| {
861-
b.iter(|| {
862-
v.iter()
863-
.tuple_combinations()
864-
.fold(0, |acc, (a, b, c, d)| acc + *a * *c - *b * *d)
865-
})
866-
});
867-
}
868-
869771
criterion_group!(
870772
benches,
871773
slice_iter,
@@ -887,7 +789,6 @@ criterion_group!(
887789
zipdot_i32_unchecked_counted_loop,
888790
zipdot_f32_unchecked_counted_loop,
889791
zip_unchecked_counted_loop3,
890-
ziplongest,
891792
group_by_lazy_1,
892793
group_by_lazy_2,
893794
slice_chunks,
@@ -903,18 +804,13 @@ criterion_group!(
903804
step_range_2,
904805
step_range_10,
905806
cartesian_product_iterator,
906-
cartesian_product_fold,
907807
multi_cartesian_product_iterator,
908-
multi_cartesian_product_fold,
909808
cartesian_product_nested_for,
910809
all_equal,
911810
all_equal_for,
912811
all_equal_default,
913812
permutations_iter,
914813
permutations_range,
915814
permutations_slice,
916-
with_position_fold,
917-
tuple_combinations_fold,
918-
while_some,
919815
);
920816
criterion_main!(benches);

benches/specializations.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use criterion::black_box;
2+
use itertools::iproduct;
23
use itertools::Itertools;
34

45
/// Create multiple functions each defining a benchmark group about iterator methods.
@@ -118,6 +119,38 @@ macro_rules! bench_specializations {
118119
// Example: To bench only `ZipLongest::fold`, you can do
119120
// cargo bench --bench specializations zip_longest/fold
120121
bench_specializations! {
122+
cartesian_product {
123+
{
124+
let v = black_box(vec![0; 16]);
125+
}
126+
iproduct!(&v, &v, &v)
127+
}
128+
multi_cartesian_product {
129+
{
130+
let vs = black_box([0; 3].map(|_| vec![0; 16]));
131+
}
132+
vs.iter().multi_cartesian_product()
133+
}
134+
tuple_combinations {
135+
{
136+
let v = black_box((0..64).collect_vec());
137+
}
138+
v.iter().tuple_combinations::<(_, _, _, _)>()
139+
}
140+
while_some {
141+
{}
142+
(0..)
143+
.map(black_box)
144+
.map(|i| char::from_digit(i, 16))
145+
.while_some()
146+
}
147+
with_position {
148+
ExactSizeIterator
149+
{
150+
let v = black_box((0..10240).collect_vec());
151+
}
152+
v.iter().with_position()
153+
}
121154
zip_longest {
122155
DoubleEndedIterator
123156
ExactSizeIterator

0 commit comments

Comments
 (0)