Skip to content

Commit

Permalink
Combinations: add benchmarks
Browse files Browse the repository at this point in the history
Add benchmarks for Combinations iterator of for return Vec sizes of 1,
2, 3, 4 and 14.

Renames TupleCombinations tests to tuple_comb_* for clarity in test
results.
  • Loading branch information
willcrozi committed Mar 1, 2019
1 parent f9a0c4e commit 925e780
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 8 deletions.
107 changes: 107 additions & 0 deletions benches/combinations.rs
@@ -0,0 +1,107 @@
#![feature(test)]

extern crate test;
extern crate itertools;

use test::{black_box, Bencher};
use itertools::Itertools;

// approximate 100_000 iterations for each combination
const N1: usize = 100_000;
const N2: usize = 448;
const N3: usize = 86;
const N4: usize = 41;
const N14: usize = 21;

#[bench]
fn combs_for01(b: &mut Bencher) {
b.iter(|| {
for i in 0..N1 {
black_box(vec![i]);
}
});
}

#[bench]
fn combs_for02(b: &mut Bencher) {
b.iter(|| {
for i in 0..N2 {
for j in (i + 1)..N2 {
black_box(vec![i, j]);
}
}
});
}

#[bench]
fn combs_for03(b: &mut Bencher) {
b.iter(|| {
for i in 0..N3 {
for j in (i + 1)..N3 {
for k in (j + 1)..N3 {
black_box(vec![i, j, k]);
}
}
}
});
}

#[bench]
fn combs_for04(b: &mut Bencher) {
b.iter(|| {
for i in 0..N4 {
for j in (i + 1)..N4 {
for k in (j + 1)..N4 {
for l in (k + 1)..N4 {
black_box(vec![i, j, k, l]);
}
}
}
}
});
}

#[bench]
fn combs_c01(b: &mut Bencher) {
b.iter(|| {
for combo in (0..N1).combinations(1) {
black_box(combo);
}
});
}

#[bench]
fn combs_c02(b: &mut Bencher) {
b.iter(|| {
for combo in (0..N2).combinations(2) {
black_box(combo);
}
});
}

#[bench]
fn combs_c03(b: &mut Bencher) {
b.iter(|| {
for combo in (0..N3).combinations(3) {
black_box(combo);
}
});
}

#[bench]
fn combs_c04(b: &mut Bencher) {
b.iter(|| {
for combo in (0..N4).combinations(4) {
black_box(combo);
}
});
}

#[bench]
fn combs_c14(b: &mut Bencher) {
b.iter(|| {
for combo in (0..N14).combinations(14) {
black_box(combo);
}
});
}
16 changes: 8 additions & 8 deletions benches/tuple_combinations.rs
Expand Up @@ -13,7 +13,7 @@ const N3: usize = 86;
const N4: usize = 41;

#[bench]
fn comb_for1(b: &mut Bencher) {
fn tuple_comb_for1(b: &mut Bencher) {
b.iter(|| {
for i in 0..N1 {
black_box(i);
Expand All @@ -22,7 +22,7 @@ fn comb_for1(b: &mut Bencher) {
}

#[bench]
fn comb_for2(b: &mut Bencher) {
fn tuple_comb_for2(b: &mut Bencher) {
b.iter(|| {
for i in 0..N2 {
for j in (i + 1)..N2 {
Expand All @@ -33,7 +33,7 @@ fn comb_for2(b: &mut Bencher) {
}

#[bench]
fn comb_for3(b: &mut Bencher) {
fn tuple_comb_for3(b: &mut Bencher) {
b.iter(|| {
for i in 0..N3 {
for j in (i + 1)..N3 {
Expand All @@ -46,7 +46,7 @@ fn comb_for3(b: &mut Bencher) {
}

#[bench]
fn comb_for4(b: &mut Bencher) {
fn tuple_comb_for4(b: &mut Bencher) {
b.iter(|| {
for i in 0..N4 {
for j in (i + 1)..N4 {
Expand All @@ -61,7 +61,7 @@ fn comb_for4(b: &mut Bencher) {
}

#[bench]
fn comb_c1(b: &mut Bencher) {
fn tuple_comb_c1(b: &mut Bencher) {
b.iter(|| {
for (i,) in (0..N1).tuple_combinations() {
black_box(i);
Expand All @@ -70,7 +70,7 @@ fn comb_c1(b: &mut Bencher) {
}

#[bench]
fn comb_c2(b: &mut Bencher) {
fn tuple_comb_c2(b: &mut Bencher) {
b.iter(|| {
for (i, j) in (0..N2).tuple_combinations() {
black_box(i + j);
Expand All @@ -79,7 +79,7 @@ fn comb_c2(b: &mut Bencher) {
}

#[bench]
fn comb_c3(b: &mut Bencher) {
fn tuple_comb_c3(b: &mut Bencher) {
b.iter(|| {
for (i, j, k) in (0..N3).tuple_combinations() {
black_box(i + j + k);
Expand All @@ -88,7 +88,7 @@ fn comb_c3(b: &mut Bencher) {
}

#[bench]
fn comb_c4(b: &mut Bencher) {
fn tuple_comb_c4(b: &mut Bencher) {
b.iter(|| {
for (i, j, k, l) in (0..N4).tuple_combinations() {
black_box(i + j + k + l);
Expand Down

0 comments on commit 925e780

Please sign in to comment.