Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark .nth[_back](n) with inputs n #916

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions benches/specializations.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#![allow(unstable_name_collisions)]

use criterion::black_box;
use itertools::iproduct;
use criterion::BenchmarkId;
use itertools::Itertools;

const NTH_INPUTS: &[usize] = &[0, 1, 2, 4, 8];

/// Create multiple functions each defining a benchmark group about iterator methods.
///
/// Each created group has functions with the following ids:
///
/// - `next`, `size_hint`, `count`, `last`, `nth`, `collect`, `fold`
/// - and when marked as `DoubleEndedIterator`: `next_back`, `rfold`
/// - and when marked as `DoubleEndedIterator`: `next_back`, `nth_back`, `rfold`
/// - and when marked as `ExactSizeIterator`: `len`
///
/// Note that this macro can be called only once.
Expand Down Expand Up @@ -73,19 +75,19 @@ macro_rules! bench_specializations {
$group.bench_function("last", |bencher| bencher.iter(|| {
$iterator.last()
}));
$group.bench_function("nth", |bencher| bencher.iter(|| {
for start in 0_usize..10 {
for n in 0..10 {
for n in NTH_INPUTS {
$group.bench_with_input(BenchmarkId::new("nth", n), n, |bencher, n| bencher.iter(|| {
for start in 0_usize..10 {
let mut it = $iterator;
if let Some(s) = start.checked_sub(1) {
black_box(it.nth(s));
}
while let Some(x) = it.nth(n) {
while let Some(x) = it.nth(*n) {
black_box(x);
}
}
}
}));
}));
}
$group.bench_function("collect", |bencher| bencher.iter(|| {
$iterator.collect::<Vec<_>>()
}));
Expand All @@ -103,19 +105,19 @@ macro_rules! bench_specializations {
black_box(x);
}
}));
$group.bench_function("nth_back", |bencher| bencher.iter(|| {
for start in 0_usize..10 {
for n in 0..10 {
for n in NTH_INPUTS {
$group.bench_with_input(BenchmarkId::new("nth_back", n), n, |bencher, n| bencher.iter(|| {
for start in 0_usize..10 {
let mut it = $iterator;
if let Some(s) = start.checked_sub(1) {
black_box(it.nth_back(s));
}
while let Some(x) = it.nth_back(n) {
while let Some(x) = it.nth_back(*n) {
black_box(x);
}
}
}
}));
}));
}
$group.bench_function("rfold", |bencher| bencher.iter(|| {
$iterator.rfold((), |(), x| {
black_box(x);
Expand All @@ -132,8 +134,11 @@ macro_rules! bench_specializations {
};
}

// Example: To bench only `ZipLongest::fold`, you can do
// Usage examples:
// - For `ZipLongest::fold` only:
// cargo bench --bench specializations zip_longest/fold
// - For `.combinations(k).nth(8)`:
// cargo bench --bench specializations combinations./nth/8
bench_specializations! {
interleave {
{
Expand Down Expand Up @@ -255,7 +260,7 @@ bench_specializations! {
{
let v = black_box(vec![0; 16]);
}
iproduct!(&v, &v, &v)
itertools::iproduct!(&v, &v, &v)
}
multi_cartesian_product {
{
Expand Down