1 | use criterion::{criterion_group, criterion_main, Criterion}; |
2 | use itertools::Itertools; |
3 | |
4 | fn s1(a: u32) -> u32 { |
5 | a |
6 | } |
7 | |
8 | fn s2(a: u32, b: u32) -> u32 { |
9 | a + b |
10 | } |
11 | |
12 | fn s3(a: u32, b: u32, c: u32) -> u32 { |
13 | a + b + c |
14 | } |
15 | |
16 | fn s4(a: u32, b: u32, c: u32, d: u32) -> u32 { |
17 | a + b + c + d |
18 | } |
19 | |
20 | fn sum_s1(s: &[u32]) -> u32 { |
21 | s1(s[0]) |
22 | } |
23 | |
24 | fn sum_s2(s: &[u32]) -> u32 { |
25 | s2(s[0], s[1]) |
26 | } |
27 | |
28 | fn sum_s3(s: &[u32]) -> u32 { |
29 | s3(s[0], s[1], s[2]) |
30 | } |
31 | |
32 | fn sum_s4(s: &[u32]) -> u32 { |
33 | s4(s[0], s[1], s[2], s[3]) |
34 | } |
35 | |
36 | fn sum_t1(s: &(&u32, )) -> u32 { |
37 | s1(*s.0) |
38 | } |
39 | |
40 | fn sum_t2(s: &(&u32, &u32)) -> u32 { |
41 | s2(*s.0, *s.1) |
42 | } |
43 | |
44 | fn sum_t3(s: &(&u32, &u32, &u32)) -> u32 { |
45 | s3(*s.0, *s.1, *s.2) |
46 | } |
47 | |
48 | fn sum_t4(s: &(&u32, &u32, &u32, &u32)) -> u32 { |
49 | s4(*s.0, *s.1, *s.2, *s.3) |
50 | } |
51 | |
52 | macro_rules! def_benchs { |
53 | ($N:expr; |
54 | $BENCH_GROUP:ident, |
55 | $TUPLE_FUN:ident, |
56 | $TUPLES:ident, |
57 | $TUPLE_WINDOWS:ident; |
58 | $SLICE_FUN:ident, |
59 | $CHUNKS:ident, |
60 | $WINDOWS:ident; |
61 | $FOR_CHUNKS:ident, |
62 | $FOR_WINDOWS:ident |
63 | ) => ( |
64 | fn $FOR_CHUNKS(c: &mut Criterion) { |
65 | let v: Vec<u32> = (0.. $N * 1_000).collect(); |
66 | let mut s = 0; |
67 | c.bench_function(&stringify!($FOR_CHUNKS).replace('_' , " " ), move |b| { |
68 | b.iter(|| { |
69 | let mut j = 0; |
70 | for _ in 0..1_000 { |
71 | s += $SLICE_FUN(&v[j..(j + $N)]); |
72 | j += $N; |
73 | } |
74 | s |
75 | }) |
76 | }); |
77 | } |
78 | |
79 | fn $FOR_WINDOWS(c: &mut Criterion) { |
80 | let v: Vec<u32> = (0..1_000).collect(); |
81 | let mut s = 0; |
82 | c.bench_function(&stringify!($FOR_WINDOWS).replace('_' , " " ), move |b| { |
83 | b.iter(|| { |
84 | for i in 0..(1_000 - $N) { |
85 | s += $SLICE_FUN(&v[i..(i + $N)]); |
86 | } |
87 | s |
88 | }) |
89 | }); |
90 | } |
91 | |
92 | fn $TUPLES(c: &mut Criterion) { |
93 | let v: Vec<u32> = (0.. $N * 1_000).collect(); |
94 | let mut s = 0; |
95 | c.bench_function(&stringify!($TUPLES).replace('_' , " " ), move |b| { |
96 | b.iter(|| { |
97 | for x in v.iter().tuples() { |
98 | s += $TUPLE_FUN(&x); |
99 | } |
100 | s |
101 | }) |
102 | }); |
103 | } |
104 | |
105 | fn $CHUNKS(c: &mut Criterion) { |
106 | let v: Vec<u32> = (0.. $N * 1_000).collect(); |
107 | let mut s = 0; |
108 | c.bench_function(&stringify!($CHUNKS).replace('_' , " " ), move |b| { |
109 | b.iter(|| { |
110 | for x in v.chunks($N) { |
111 | s += $SLICE_FUN(x); |
112 | } |
113 | s |
114 | }) |
115 | }); |
116 | } |
117 | |
118 | fn $TUPLE_WINDOWS(c: &mut Criterion) { |
119 | let v: Vec<u32> = (0..1_000).collect(); |
120 | let mut s = 0; |
121 | c.bench_function(&stringify!($TUPLE_WINDOWS).replace('_' , " " ), move |b| { |
122 | b.iter(|| { |
123 | for x in v.iter().tuple_windows() { |
124 | s += $TUPLE_FUN(&x); |
125 | } |
126 | s |
127 | }) |
128 | }); |
129 | } |
130 | |
131 | fn $WINDOWS(c: &mut Criterion) { |
132 | let v: Vec<u32> = (0..1_000).collect(); |
133 | let mut s = 0; |
134 | c.bench_function(&stringify!($WINDOWS).replace('_' , " " ), move |b| { |
135 | b.iter(|| { |
136 | for x in v.windows($N) { |
137 | s += $SLICE_FUN(x); |
138 | } |
139 | s |
140 | }) |
141 | }); |
142 | } |
143 | |
144 | criterion_group!( |
145 | $BENCH_GROUP, |
146 | $FOR_CHUNKS, |
147 | $FOR_WINDOWS, |
148 | $TUPLES, |
149 | $CHUNKS, |
150 | $TUPLE_WINDOWS, |
151 | $WINDOWS, |
152 | ); |
153 | ) |
154 | } |
155 | |
156 | def_benchs!{ |
157 | 1; |
158 | benches_1, |
159 | sum_t1, |
160 | tuple_chunks_1, |
161 | tuple_windows_1; |
162 | sum_s1, |
163 | slice_chunks_1, |
164 | slice_windows_1; |
165 | for_chunks_1, |
166 | for_windows_1 |
167 | } |
168 | |
169 | def_benchs!{ |
170 | 2; |
171 | benches_2, |
172 | sum_t2, |
173 | tuple_chunks_2, |
174 | tuple_windows_2; |
175 | sum_s2, |
176 | slice_chunks_2, |
177 | slice_windows_2; |
178 | for_chunks_2, |
179 | for_windows_2 |
180 | } |
181 | |
182 | def_benchs!{ |
183 | 3; |
184 | benches_3, |
185 | sum_t3, |
186 | tuple_chunks_3, |
187 | tuple_windows_3; |
188 | sum_s3, |
189 | slice_chunks_3, |
190 | slice_windows_3; |
191 | for_chunks_3, |
192 | for_windows_3 |
193 | } |
194 | |
195 | def_benchs!{ |
196 | 4; |
197 | benches_4, |
198 | sum_t4, |
199 | tuple_chunks_4, |
200 | tuple_windows_4; |
201 | sum_s4, |
202 | slice_chunks_4, |
203 | slice_windows_4; |
204 | for_chunks_4, |
205 | for_windows_4 |
206 | } |
207 | |
208 | criterion_main!( |
209 | benches_1, |
210 | benches_2, |
211 | benches_3, |
212 | benches_4, |
213 | ); |
214 | |