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 | |
10 | fn tuple_comb_for1(c: &mut Criterion) { |
11 | c.bench_function("tuple comb for1" , move |b| { |
12 | b.iter(|| { |
13 | for i in 0..N1 { |
14 | black_box(i); |
15 | } |
16 | }) |
17 | }); |
18 | } |
19 | |
20 | fn tuple_comb_for2(c: &mut Criterion) { |
21 | c.bench_function("tuple 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 | |
32 | fn tuple_comb_for3(c: &mut Criterion) { |
33 | c.bench_function("tuple 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 | |
46 | fn tuple_comb_for4(c: &mut Criterion) { |
47 | c.bench_function("tuple 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 | |
62 | fn tuple_comb_c1(c: &mut Criterion) { |
63 | c.bench_function("tuple comb c1" , move |b| { |
64 | b.iter(|| { |
65 | for (i,) in (0..N1).tuple_combinations() { |
66 | black_box(i); |
67 | } |
68 | }) |
69 | }); |
70 | } |
71 | |
72 | fn tuple_comb_c2(c: &mut Criterion) { |
73 | c.bench_function("tuple 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 | |
82 | fn tuple_comb_c3(c: &mut Criterion) { |
83 | c.bench_function("tuple 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 | |
92 | fn tuple_comb_c4(c: &mut Criterion) { |
93 | c.bench_function("tuple 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 | |
102 | criterion_group!( |
103 | benches, |
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, |
112 | ); |
113 | criterion_main!(benches); |
114 | |