Skip to content

Commit 2b005c2

Browse files
committed
Add benchmarks for Combinations
Renames tuple_combinations benchmark functions to tuple_comb_* for clarity in test results.
1 parent 4114081 commit 2b005c2

File tree

3 files changed

+153
-24
lines changed

3 files changed

+153
-24
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ harness = false
6767
[[bench]]
6868
name = "bench1"
6969
harness = false
70+
71+
[[bench]]
72+
name = "combinations"
73+
harness = false

benches/combinations.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
use itertools::Itertools;
3+
4+
// approximate 100_000 iterations for each combination
5+
const N1: usize = 100_000;
6+
const N2: usize = 448;
7+
const N3: usize = 86;
8+
const N4: usize = 41;
9+
const N14: usize = 21;
10+
11+
fn comb_for1(c: &mut Criterion) {
12+
c.bench_function("comb for1", move |b| {
13+
b.iter(|| {
14+
for i in 0..N1 {
15+
black_box(vec![i]);
16+
}
17+
})
18+
});
19+
}
20+
21+
fn comb_for2(c: &mut Criterion) {
22+
c.bench_function("comb for2", move |b| {
23+
b.iter(|| {
24+
for i in 0..N2 {
25+
for j in (i + 1)..N2 {
26+
black_box(vec![i, j]);
27+
}
28+
}
29+
})
30+
});
31+
}
32+
33+
fn comb_for3(c: &mut Criterion) {
34+
c.bench_function("comb for3", move |b| {
35+
b.iter(|| {
36+
for i in 0..N3 {
37+
for j in (i + 1)..N3 {
38+
for k in (j + 1)..N3 {
39+
black_box(vec![i, j, k]);
40+
}
41+
}
42+
}
43+
})
44+
});
45+
}
46+
47+
fn comb_for4(c: &mut Criterion) {
48+
c.bench_function("comb for4", move |b| {
49+
b.iter(|| {
50+
for i in 0..N4 {
51+
for j in (i + 1)..N4 {
52+
for k in (j + 1)..N4 {
53+
for l in (k + 1)..N4 {
54+
black_box(vec![i, j, k, l]);
55+
}
56+
}
57+
}
58+
}
59+
})
60+
});
61+
}
62+
63+
fn comb_c1(c: &mut Criterion) {
64+
c.bench_function("comb c1", move |b| {
65+
b.iter(|| {
66+
for combo in (0..N1).combinations(1) {
67+
black_box(combo);
68+
}
69+
})
70+
});
71+
}
72+
73+
fn comb_c2(c: &mut Criterion) {
74+
c.bench_function("comb c2", move |b| {
75+
b.iter(|| {
76+
for combo in (0..N2).combinations(2) {
77+
black_box(combo);
78+
}
79+
})
80+
});
81+
}
82+
83+
fn comb_c3(c: &mut Criterion) {
84+
c.bench_function("comb c3", move |b| {
85+
b.iter(|| {
86+
for combo in (0..N3).combinations(3) {
87+
black_box(combo);
88+
}
89+
})
90+
});
91+
}
92+
93+
fn comb_c4(c: &mut Criterion) {
94+
c.bench_function("comb c4", move |b| {
95+
b.iter(|| {
96+
for combo in (0..N4).combinations(4) {
97+
black_box(combo);
98+
}
99+
})
100+
});
101+
}
102+
103+
fn comb_c14(c: &mut Criterion) {
104+
c.bench_function("comb c14", move |b| {
105+
b.iter(|| {
106+
for combo in (0..N14).combinations(14) {
107+
black_box(combo);
108+
}
109+
})
110+
});
111+
}
112+
113+
criterion_group!(
114+
benches,
115+
comb_for1,
116+
comb_for2,
117+
comb_for3,
118+
comb_for4,
119+
comb_c1,
120+
comb_c2,
121+
comb_c3,
122+
comb_c4,
123+
comb_c14,
124+
);
125+
criterion_main!(benches);

benches/tuple_combinations.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const N2: usize = 448;
77
const N3: usize = 86;
88
const N4: usize = 41;
99

10-
fn comb_for1(c: &mut Criterion) {
11-
c.bench_function("comb for1", move |b| {
10+
fn tuple_comb_for1(c: &mut Criterion) {
11+
c.bench_function("tuple comb for1", move |b| {
1212
b.iter(|| {
1313
for i in 0..N1 {
1414
black_box(i);
@@ -17,8 +17,8 @@ fn comb_for1(c: &mut Criterion) {
1717
});
1818
}
1919

20-
fn comb_for2(c: &mut Criterion) {
21-
c.bench_function("comb for2", move |b| {
20+
fn tuple_comb_for2(c: &mut Criterion) {
21+
c.bench_function("tuple comb for2", move |b| {
2222
b.iter(|| {
2323
for i in 0..N2 {
2424
for j in (i + 1)..N2 {
@@ -29,8 +29,8 @@ fn comb_for2(c: &mut Criterion) {
2929
});
3030
}
3131

32-
fn comb_for3(c: &mut Criterion) {
33-
c.bench_function("comb for3", move |b| {
32+
fn tuple_comb_for3(c: &mut Criterion) {
33+
c.bench_function("tuple comb for3", move |b| {
3434
b.iter(|| {
3535
for i in 0..N3 {
3636
for j in (i + 1)..N3 {
@@ -43,8 +43,8 @@ fn comb_for3(c: &mut Criterion) {
4343
});
4444
}
4545

46-
fn comb_for4(c: &mut Criterion) {
47-
c.bench_function("comb for4", move |b| {
46+
fn tuple_comb_for4(c: &mut Criterion) {
47+
c.bench_function("tuple comb for4", move |b| {
4848
b.iter(|| {
4949
for i in 0..N4 {
5050
for j in (i + 1)..N4 {
@@ -59,8 +59,8 @@ fn comb_for4(c: &mut Criterion) {
5959
});
6060
}
6161

62-
fn comb_c1(c: &mut Criterion) {
63-
c.bench_function("comb c1", move |b| {
62+
fn tuple_comb_c1(c: &mut Criterion) {
63+
c.bench_function("tuple comb c1", move |b| {
6464
b.iter(|| {
6565
for (i,) in (0..N1).tuple_combinations() {
6666
black_box(i);
@@ -69,8 +69,8 @@ fn comb_c1(c: &mut Criterion) {
6969
});
7070
}
7171

72-
fn comb_c2(c: &mut Criterion) {
73-
c.bench_function("comb c2", move |b| {
72+
fn tuple_comb_c2(c: &mut Criterion) {
73+
c.bench_function("tuple comb c2", move |b| {
7474
b.iter(|| {
7575
for (i, j) in (0..N2).tuple_combinations() {
7676
black_box(i + j);
@@ -79,8 +79,8 @@ fn comb_c2(c: &mut Criterion) {
7979
});
8080
}
8181

82-
fn comb_c3(c: &mut Criterion) {
83-
c.bench_function("comb c3", move |b| {
82+
fn tuple_comb_c3(c: &mut Criterion) {
83+
c.bench_function("tuple comb c3", move |b| {
8484
b.iter(|| {
8585
for (i, j, k) in (0..N3).tuple_combinations() {
8686
black_box(i + j + k);
@@ -89,8 +89,8 @@ fn comb_c3(c: &mut Criterion) {
8989
});
9090
}
9191

92-
fn comb_c4(c: &mut Criterion) {
93-
c.bench_function("comb c4", move |b| {
92+
fn tuple_comb_c4(c: &mut Criterion) {
93+
c.bench_function("tuple comb c4", move |b| {
9494
b.iter(|| {
9595
for (i, j, k, l) in (0..N4).tuple_combinations() {
9696
black_box(i + j + k + l);
@@ -101,13 +101,13 @@ fn comb_c4(c: &mut Criterion) {
101101

102102
criterion_group!(
103103
benches,
104-
comb_for1,
105-
comb_for2,
106-
comb_for3,
107-
comb_for4,
108-
comb_c1,
109-
comb_c2,
110-
comb_c3,
111-
comb_c4,
104+
tuple_comb_for1,
105+
tuple_comb_for2,
106+
tuple_comb_for3,
107+
tuple_comb_for4,
108+
tuple_comb_c1,
109+
tuple_comb_c2,
110+
tuple_comb_c3,
111+
tuple_comb_c4,
112112
);
113113
criterion_main!(benches);

0 commit comments

Comments
 (0)