blob: 84411efd80df6ebfec5598c562d76e9079002ef8 [file] [log] [blame]
Jakub Kotura425e552020-12-21 17:28:15 +01001use 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;
9
10fn comb_for1(c: &mut Criterion) {
11 c.bench_function("comb for1", move |b| {
12 b.iter(|| {
13 for i in 0..N1 {
14 black_box(i);
15 }
16 })
17 });
18}
19
20fn comb_for2(c: &mut Criterion) {
21 c.bench_function("comb for2", move |b| {
22 b.iter(|| {
23 for i in 0..N2 {
24 for j in (i + 1)..N2 {
25 black_box(i + j);
26 }
27 }
28 })
29 });
30}
31
32fn comb_for3(c: &mut Criterion) {
33 c.bench_function("comb for3", move |b| {
34 b.iter(|| {
35 for i in 0..N3 {
36 for j in (i + 1)..N3 {
37 for k in (j + 1)..N3 {
38 black_box(i + j + k);
39 }
40 }
41 }
42 })
43 });
44}
45
46fn comb_for4(c: &mut Criterion) {
47 c.bench_function("comb for4", move |b| {
48 b.iter(|| {
49 for i in 0..N4 {
50 for j in (i + 1)..N4 {
51 for k in (j + 1)..N4 {
52 for l in (k + 1)..N4 {
53 black_box(i + j + k + l);
54 }
55 }
56 }
57 }
58 })
59 });
60}
61
62fn comb_c1(c: &mut Criterion) {
63 c.bench_function("comb c1", move |b| {
64 b.iter(|| {
65 for (i,) in (0..N1).tuple_combinations() {
66 black_box(i);
67 }
68 })
69 });
70}
71
72fn comb_c2(c: &mut Criterion) {
73 c.bench_function("comb c2", move |b| {
74 b.iter(|| {
75 for (i, j) in (0..N2).tuple_combinations() {
76 black_box(i + j);
77 }
78 })
79 });
80}
81
82fn comb_c3(c: &mut Criterion) {
83 c.bench_function("comb c3", move |b| {
84 b.iter(|| {
85 for (i, j, k) in (0..N3).tuple_combinations() {
86 black_box(i + j + k);
87 }
88 })
89 });
90}
91
92fn comb_c4(c: &mut Criterion) {
93 c.bench_function("comb c4", move |b| {
94 b.iter(|| {
95 for (i, j, k, l) in (0..N4).tuple_combinations() {
96 black_box(i + j + k + l);
97 }
98 })
99 });
100}
101
102criterion_group!(
103 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,
112);
113criterion_main!(benches);