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 
comb_for1(c: &mut Criterion)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 
comb_for2(c: &mut Criterion)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 
comb_for3(c: &mut Criterion)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 
comb_for4(c: &mut Criterion)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 
comb_c1(c: &mut Criterion)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 
comb_c2(c: &mut Criterion)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 
comb_c3(c: &mut Criterion)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 
comb_c4(c: &mut Criterion)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 
comb_c14(c: &mut Criterion)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, comb_for1, comb_for2, comb_for3, comb_for4, comb_c1, comb_c2, comb_c3, comb_c4,
115     comb_c14,
116 );
117 criterion_main!(benches);
118