From 5a676ab45fee9818e943a50dff76cd85317bb9ef Mon Sep 17 00:00:00 2001 From: Will Crozier Date: Sun, 27 Sep 2020 02:28:11 +0100 Subject: [PATCH] Add benchmarks for Combinations and Powerset Renames tuple_combinations benchmark functions to tuple_comb_* for clarity in test results. --- Cargo.toml | 8 +++ benches/combinations.rs | 125 ++++++++++++++++++++++++++++++++++ benches/powerset.rs | 44 ++++++++++++ benches/tuple_combinations.rs | 48 ++++++------- 4 files changed, 201 insertions(+), 24 deletions(-) create mode 100644 benches/combinations.rs create mode 100644 benches/powerset.rs diff --git a/Cargo.toml b/Cargo.toml index 3de576bc3..c6131625e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,3 +67,11 @@ harness = false [[bench]] name = "bench1" harness = false + +[[bench]] +name = "combinations" +harness = false + +[[bench]] +name = "powerset" +harness = false diff --git a/benches/combinations.rs b/benches/combinations.rs new file mode 100644 index 000000000..e7433a4cb --- /dev/null +++ b/benches/combinations.rs @@ -0,0 +1,125 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +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; + +fn comb_for1(c: &mut Criterion) { + c.bench_function("comb for1", move |b| { + b.iter(|| { + for i in 0..N1 { + black_box(vec![i]); + } + }) + }); +} + +fn comb_for2(c: &mut Criterion) { + c.bench_function("comb for2", move |b| { + b.iter(|| { + for i in 0..N2 { + for j in (i + 1)..N2 { + black_box(vec![i, j]); + } + } + }) + }); +} + +fn comb_for3(c: &mut Criterion) { + c.bench_function("comb for3", move |b| { + 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]); + } + } + } + }) + }); +} + +fn comb_for4(c: &mut Criterion) { + c.bench_function("comb for4", move |b| { + 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]); + } + } + } + } + }) + }); +} + +fn comb_c1(c: &mut Criterion) { + c.bench_function("comb c1", move |b| { + b.iter(|| { + for combo in (0..N1).combinations(1) { + black_box(combo); + } + }) + }); +} + +fn comb_c2(c: &mut Criterion) { + c.bench_function("comb c2", move |b| { + b.iter(|| { + for combo in (0..N2).combinations(2) { + black_box(combo); + } + }) + }); +} + +fn comb_c3(c: &mut Criterion) { + c.bench_function("comb c3", move |b| { + b.iter(|| { + for combo in (0..N3).combinations(3) { + black_box(combo); + } + }) + }); +} + +fn comb_c4(c: &mut Criterion) { + c.bench_function("comb c4", move |b| { + b.iter(|| { + for combo in (0..N4).combinations(4) { + black_box(combo); + } + }) + }); +} + +fn comb_c14(c: &mut Criterion) { + c.bench_function("comb c14", move |b| { + b.iter(|| { + for combo in (0..N14).combinations(14) { + black_box(combo); + } + }) + }); +} + +criterion_group!( + benches, + comb_for1, + comb_for2, + comb_for3, + comb_for4, + comb_c1, + comb_c2, + comb_c3, + comb_c4, + comb_c14, +); +criterion_main!(benches); diff --git a/benches/powerset.rs b/benches/powerset.rs new file mode 100644 index 000000000..074550bc4 --- /dev/null +++ b/benches/powerset.rs @@ -0,0 +1,44 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use itertools::Itertools; + +// Keep aggregate generated elements the same, regardless of powerset length. +const TOTAL_ELEMENTS: usize = 1 << 12; +const fn calc_iters(n: usize) -> usize { + TOTAL_ELEMENTS / (1 << n) +} + +fn powerset_n(c: &mut Criterion, n: usize) { + let id = format!("powerset {}", n); + c.bench_function(id.as_str(), move |b| { + b.iter(|| { + for _ in 0..calc_iters(n) { + for elt in (0..n).powerset() { + black_box(elt); + } + } + }) + }); +} + +fn powerset_0(c: &mut Criterion) { powerset_n(c, 0); } + +fn powerset_1(c: &mut Criterion) { powerset_n(c, 1); } + +fn powerset_2(c: &mut Criterion) { powerset_n(c, 2); } + +fn powerset_4(c: &mut Criterion) { powerset_n(c, 4); } + +fn powerset_8(c: &mut Criterion) { powerset_n(c, 8); } + +fn powerset_12(c: &mut Criterion) { powerset_n(c, 12); } + +criterion_group!( + benches, + powerset_0, + powerset_1, + powerset_2, + powerset_4, + powerset_8, + powerset_12, +); +criterion_main!(benches); \ No newline at end of file diff --git a/benches/tuple_combinations.rs b/benches/tuple_combinations.rs index 84411efd8..4e26b282e 100644 --- a/benches/tuple_combinations.rs +++ b/benches/tuple_combinations.rs @@ -7,8 +7,8 @@ const N2: usize = 448; const N3: usize = 86; const N4: usize = 41; -fn comb_for1(c: &mut Criterion) { - c.bench_function("comb for1", move |b| { +fn tuple_comb_for1(c: &mut Criterion) { + c.bench_function("tuple comb for1", move |b| { b.iter(|| { for i in 0..N1 { black_box(i); @@ -17,8 +17,8 @@ fn comb_for1(c: &mut Criterion) { }); } -fn comb_for2(c: &mut Criterion) { - c.bench_function("comb for2", move |b| { +fn tuple_comb_for2(c: &mut Criterion) { + c.bench_function("tuple comb for2", move |b| { b.iter(|| { for i in 0..N2 { for j in (i + 1)..N2 { @@ -29,8 +29,8 @@ fn comb_for2(c: &mut Criterion) { }); } -fn comb_for3(c: &mut Criterion) { - c.bench_function("comb for3", move |b| { +fn tuple_comb_for3(c: &mut Criterion) { + c.bench_function("tuple comb for3", move |b| { b.iter(|| { for i in 0..N3 { for j in (i + 1)..N3 { @@ -43,8 +43,8 @@ fn comb_for3(c: &mut Criterion) { }); } -fn comb_for4(c: &mut Criterion) { - c.bench_function("comb for4", move |b| { +fn tuple_comb_for4(c: &mut Criterion) { + c.bench_function("tuple comb for4", move |b| { b.iter(|| { for i in 0..N4 { for j in (i + 1)..N4 { @@ -59,8 +59,8 @@ fn comb_for4(c: &mut Criterion) { }); } -fn comb_c1(c: &mut Criterion) { - c.bench_function("comb c1", move |b| { +fn tuple_comb_c1(c: &mut Criterion) { + c.bench_function("tuple comb c1", move |b| { b.iter(|| { for (i,) in (0..N1).tuple_combinations() { black_box(i); @@ -69,8 +69,8 @@ fn comb_c1(c: &mut Criterion) { }); } -fn comb_c2(c: &mut Criterion) { - c.bench_function("comb c2", move |b| { +fn tuple_comb_c2(c: &mut Criterion) { + c.bench_function("tuple comb c2", move |b| { b.iter(|| { for (i, j) in (0..N2).tuple_combinations() { black_box(i + j); @@ -79,8 +79,8 @@ fn comb_c2(c: &mut Criterion) { }); } -fn comb_c3(c: &mut Criterion) { - c.bench_function("comb c3", move |b| { +fn tuple_comb_c3(c: &mut Criterion) { + c.bench_function("tuple comb c3", move |b| { b.iter(|| { for (i, j, k) in (0..N3).tuple_combinations() { black_box(i + j + k); @@ -89,8 +89,8 @@ fn comb_c3(c: &mut Criterion) { }); } -fn comb_c4(c: &mut Criterion) { - c.bench_function("comb c4", move |b| { +fn tuple_comb_c4(c: &mut Criterion) { + c.bench_function("tuple comb c4", move |b| { b.iter(|| { for (i, j, k, l) in (0..N4).tuple_combinations() { black_box(i + j + k + l); @@ -101,13 +101,13 @@ fn comb_c4(c: &mut Criterion) { criterion_group!( benches, - comb_for1, - comb_for2, - comb_for3, - comb_for4, - comb_c1, - comb_c2, - comb_c3, - comb_c4, + tuple_comb_for1, + tuple_comb_for2, + tuple_comb_for3, + tuple_comb_for4, + tuple_comb_c1, + tuple_comb_c2, + tuple_comb_c3, + tuple_comb_c4, ); criterion_main!(benches);