blob: e7433a4cb099dac5042c800ce4ef0596edce62cb [file] [log] [blame]
Joel Galenson6f798712021-04-01 17:03:06 -07001use criterion::{black_box, criterion_group, criterion_main, Criterion};
2use itertools::Itertools;
3
4// approximate 100_000 iterations for each combination
5const N1: usize = 100_000;
6const N2: usize = 448;
7const N3: usize = 86;
8const N4: usize = 41;
9const N14: usize = 21;
10
11fn 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
21fn 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
33fn 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
47fn 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
63fn 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
73fn 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
83fn 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
93fn 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
103fn 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
113criterion_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);
125criterion_main!(benches);