blob: 6c0148b1ae81a185e11182e9f95907e4efbafd98 [file] [log] [blame]
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02001#![cfg(test)]
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02002
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +02003extern crate criterion;
4extern crate criterion_bencher_compat;
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02005extern crate num;
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02006
Matthias Andreas Benkard50327572020-06-30 22:53:44 +02007use rust_samples::swapper::{
8 CAllocaSwapper, CLoopSwapper, CMallocSwapper, LoopSwapper, PtrSwapper, Swapper,
9};
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020010
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020011use criterion::measurement::WallTime;
12use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020013use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000};
14use num::{one, zero, Num};
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020015
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020016fn bench_swap<S, T, N>(b: &mut Bencher<WallTime>)
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020017where
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020018 S: Swapper,
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020019 N: heapless::ArrayLength<T>,
20 T: Num + Copy,
21{
22 let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
23
24 b.iter(|| {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020025 S::swap(&mut arr1, &mut arr2);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020026 black_box(&arr1);
27 black_box(&arr2);
28 });
29}
30
31/// Allocates two vectors of fixed length on the heap.
32fn bench_swap_setup<T, N>() -> (Vec<T>, Vec<T>)
33where
34 N: heapless::ArrayLength<T>,
35 T: Num + Clone,
36{
37 let mut arr1: Vec<T> = Vec::new();
38 let mut arr2 = arr1.clone();
39
40 arr1.resize(N::to_usize(), black_box(zero()));
41 arr2.resize(N::to_usize(), black_box(one()));
42
43 (arr1, arr2)
44}
45
46/// A heapless version of `bench_swap_setup`.
47fn _bench_swap_setup_heapless<T, N>() -> (heapless::Vec<T, N>, heapless::Vec<T, N>)
48where
49 N: heapless::ArrayLength<T>,
50 T: Num + Clone,
51{
52 let mut arr1: heapless::Vec<T, N> = heapless::Vec::new();
53 let mut arr2 = arr1.clone();
54
55 arr1.resize(N::to_usize(), black_box(zero()))
56 .expect("insufficient stack space");
57 arr2.resize(N::to_usize(), black_box(one()))
58 .expect("insufficient stack space");
59
60 (arr1, arr2)
61}
62
63type BenchInt = u8;
64
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020065fn bench_swap_loop_len_10(c: &mut Criterion) {
66 c.bench_function("swap_loop_len_10", |b| {
67 bench_swap::<LoopSwapper, BenchInt, U10>(b)
68 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020069}
70
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020071fn bench_swap_loop_len_100(c: &mut Criterion) {
72 c.bench_function("swap_loop_len_100", |b| {
73 bench_swap::<LoopSwapper, BenchInt, U100>(b)
74 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020075}
76
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020077fn bench_swap_loop_len_1000(c: &mut Criterion) {
78 c.bench_function("swap_loop_len_1000", |b| {
79 bench_swap::<LoopSwapper, BenchInt, U1000>(b)
80 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020081}
82
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020083fn bench_swap_loop_len_10000(c: &mut Criterion) {
84 c.bench_function("swap_loop_len_10000", |b| {
85 bench_swap::<LoopSwapper, BenchInt, U10000>(b)
86 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020087}
88
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020089fn bench_swap_loop_len_100000(c: &mut Criterion) {
90 c.bench_function("swap_loop_len_100000", |b| {
91 bench_swap::<LoopSwapper, BenchInt, U100000>(b)
92 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020093}
94
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +020095fn bench_swap_loop_len_1000000(c: &mut Criterion) {
96 c.bench_function("swap_loop_len_1000000", |b| {
97 bench_swap::<LoopSwapper, BenchInt, U1000000>(b)
98 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020099}
100
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200101fn bench_swap_ptrswap_len_10(c: &mut Criterion) {
102 c.bench_function("swap_ptrswap_len_10", |b| {
103 bench_swap::<PtrSwapper, BenchInt, U10>(b)
104 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200105}
106
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200107fn bench_swap_ptrswap_len_100(c: &mut Criterion) {
108 c.bench_function("swap_ptrswap_len_100", |b| {
109 bench_swap::<PtrSwapper, BenchInt, U100>(b)
110 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200111}
112
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200113fn bench_swap_ptrswap_len_1000(c: &mut Criterion) {
114 c.bench_function("swap_ptrswap_len_1000", |b| {
115 bench_swap::<PtrSwapper, BenchInt, U1000>(b)
116 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200117}
118
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200119fn bench_swap_ptrswap_len_10000(c: &mut Criterion) {
120 c.bench_function("swap_ptrswap_len_10000", |b| {
121 bench_swap::<PtrSwapper, BenchInt, U10000>(b)
122 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200123}
124
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200125fn bench_swap_ptrswap_len_100000(c: &mut Criterion) {
126 c.bench_function("swap_ptrswap_len_100000", |b| {
127 bench_swap::<PtrSwapper, BenchInt, U100000>(b)
128 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200129}
130
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200131fn bench_swap_ptrswap_len_1000000(c: &mut Criterion) {
132 c.bench_function("swap_ptrswap_len_1000000", |b| {
133 bench_swap::<PtrSwapper, BenchInt, U1000000>(b)
134 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200135}
136
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200137fn bench_cswap_loop_len_10(c: &mut Criterion) {
138 c.bench_function("cswap_loop_len_10", |b| {
139 bench_swap::<CLoopSwapper, BenchInt, U10>(b)
140 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200141}
142
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200143fn bench_cswap_loop_len_100(c: &mut Criterion) {
144 c.bench_function("cswap_loop_len_100", |b| {
145 bench_swap::<CLoopSwapper, BenchInt, U100>(b)
146 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200147}
148
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200149fn bench_cswap_loop_len_1000(c: &mut Criterion) {
150 c.bench_function("cswap_loop_len_1000", |b| {
151 bench_swap::<CLoopSwapper, BenchInt, U1000>(b)
152 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200153}
154
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200155fn bench_cswap_loop_len_10000(c: &mut Criterion) {
156 c.bench_function("cswap_loop_len_10000", |b| {
157 bench_swap::<CLoopSwapper, BenchInt, U10000>(b)
158 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200159}
160
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200161fn bench_cswap_loop_len_100000(c: &mut Criterion) {
162 c.bench_function("cswap_loop_len_100000", |b| {
163 bench_swap::<CLoopSwapper, BenchInt, U100000>(b)
164 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200165}
166
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200167fn bench_cswap_loop_len_1000000(c: &mut Criterion) {
168 c.bench_function("cswap_loop_len_1000000", |b| {
169 bench_swap::<CLoopSwapper, BenchInt, U1000000>(b)
170 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200171}
172
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200173fn bench_cswap_alloca_len_10(c: &mut Criterion) {
174 c.bench_function("cswap_alloca_len_10", |b| {
175 bench_swap::<CAllocaSwapper, BenchInt, U10>(b)
176 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200177}
178
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200179fn bench_cswap_alloca_len_100(c: &mut Criterion) {
180 c.bench_function("cswap_alloca_len_100", |b| {
181 bench_swap::<CAllocaSwapper, BenchInt, U100>(b)
182 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200183}
184
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200185fn bench_cswap_alloca_len_1000(c: &mut Criterion) {
186 c.bench_function("cswap_alloca_len_1000", |b| {
187 bench_swap::<CAllocaSwapper, BenchInt, U1000>(b)
188 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200189}
190
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200191fn bench_cswap_alloca_len_10000(c: &mut Criterion) {
192 c.bench_function("cswap_alloca_len_10000", |b| {
193 bench_swap::<CAllocaSwapper, BenchInt, U10000>(b)
194 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200195}
196
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200197fn bench_cswap_alloca_len_100000(c: &mut Criterion) {
198 c.bench_function("cswap_alloca_len_100000", |b| {
199 bench_swap::<CAllocaSwapper, BenchInt, U100000>(b)
200 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200201}
202
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200203fn bench_cswap_alloca_len_1000000(c: &mut Criterion) {
204 c.bench_function("cswap_alloca_len_1000000", |b| {
205 bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b)
206 });
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200207}
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200208
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200209fn bench_cswap_malloc_len_10(c: &mut Criterion) {
210 c.bench_function("cswap_malloc_len_10", |b| {
211 bench_swap::<CMallocSwapper, BenchInt, U10>(b)
212 });
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200213}
214
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200215fn bench_cswap_malloc_len_100(c: &mut Criterion) {
216 c.bench_function("cswap_malloc_len_100", |b| {
217 bench_swap::<CMallocSwapper, BenchInt, U100>(b)
218 });
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200219}
220
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200221fn bench_cswap_malloc_len_1000(c: &mut Criterion) {
222 c.bench_function("cswap_malloc_len_1000", |b| {
223 bench_swap::<CMallocSwapper, BenchInt, U1000>(b)
224 });
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200225}
226
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200227fn bench_cswap_malloc_len_10000(c: &mut Criterion) {
228 c.bench_function("cswap_malloc_len_10000", |b| {
229 bench_swap::<CMallocSwapper, BenchInt, U10000>(b)
230 });
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200231}
232
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200233fn bench_cswap_malloc_len_100000(c: &mut Criterion) {
234 c.bench_function("cswap_malloc_len_100000", |b| {
235 bench_swap::<CMallocSwapper, BenchInt, U100000>(b)
236 });
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200237}
238
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200239fn bench_cswap_malloc_len_1000000(c: &mut Criterion) {
240 c.bench_function("cswap_malloc_len_1000000", |b| {
241 bench_swap::<CMallocSwapper, BenchInt, U1000000>(b)
242 });
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200243}
Matthias Andreas Benkard49de2bb2020-06-30 23:12:08 +0200244
245criterion_group!(
246 benches,
247 bench_swap_loop_len_10,
248 bench_swap_loop_len_100,
249 bench_swap_loop_len_1000,
250 bench_swap_loop_len_10000,
251 bench_swap_loop_len_100000,
252 bench_swap_loop_len_1000000,
253 bench_swap_ptrswap_len_10,
254 bench_swap_ptrswap_len_100,
255 bench_swap_ptrswap_len_1000,
256 bench_swap_ptrswap_len_10000,
257 bench_swap_ptrswap_len_100000,
258 bench_swap_ptrswap_len_1000000,
259 bench_cswap_loop_len_10,
260 bench_cswap_loop_len_100,
261 bench_cswap_loop_len_1000,
262 bench_cswap_loop_len_10000,
263 bench_cswap_loop_len_100000,
264 bench_cswap_loop_len_1000000,
265 bench_cswap_alloca_len_10,
266 bench_cswap_alloca_len_100,
267 bench_cswap_alloca_len_1000,
268 bench_cswap_alloca_len_10000,
269 bench_cswap_alloca_len_100000,
270 bench_cswap_alloca_len_1000000,
271 bench_cswap_malloc_len_10,
272 bench_cswap_malloc_len_100,
273 bench_cswap_malloc_len_1000,
274 bench_cswap_malloc_len_10000,
275 bench_cswap_malloc_len_100000,
276 bench_cswap_malloc_len_1000000
277);
278criterion_main!(benches);