blob: 3f693693f8c40ea3a22ee69b4a5c0d8b0e432477 [file] [log] [blame]
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02001#![cfg(test)]
2#![feature(test)]
3#![feature(slice_fill)]
4
5extern crate num;
6extern crate test;
7
8use rust_samples::{cswap, swap};
9
10use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000};
11use num::{one, zero, Num};
12use test::{black_box, Bencher};
13
14fn bench_swap_loop<T, N>(b: &mut Bencher)
15where
16 N: heapless::ArrayLength<T>,
17 T: Num + Copy,
18{
19 let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
20
21 b.iter(|| {
22 swap::swap_loop(&mut arr1, &mut arr2);
23 black_box(&arr1);
24 black_box(&arr2);
25 });
26}
27
28fn bench_swap_ptrswap<T, N>(b: &mut Bencher)
29where
30 N: heapless::ArrayLength<T>,
31 T: Num + Copy,
32{
33 let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
34
35 b.iter(|| {
36 swap::swap_ptrswap(&mut arr1, &mut arr2);
37 black_box(&arr1);
38 black_box(&arr2);
39 });
40}
41
42fn bench_cswap_loop<T, N>(b: &mut Bencher)
43where
44 N: heapless::ArrayLength<T>,
45 T: Num + Copy,
46{
47 let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
48
49 b.iter(|| {
50 cswap::cswap_loop(&mut arr1, &mut arr2);
51 black_box(&arr1);
52 black_box(&arr2);
53 });
54}
55
56fn bench_cswap_alloca<T, N>(b: &mut Bencher)
57where
58 N: heapless::ArrayLength<T>,
59 T: Num + Copy,
60{
61 let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
62
63 b.iter(|| {
64 cswap::cswap_alloca(&mut arr1, &mut arr2);
65 black_box(&arr1);
66 black_box(&arr2);
67 });
68}
69
70/// Allocates two vectors of fixed length on the heap.
71fn bench_swap_setup<T, N>() -> (Vec<T>, Vec<T>)
72where
73 N: heapless::ArrayLength<T>,
74 T: Num + Clone,
75{
76 let mut arr1: Vec<T> = Vec::new();
77 let mut arr2 = arr1.clone();
78
79 arr1.resize(N::to_usize(), black_box(zero()));
80 arr2.resize(N::to_usize(), black_box(one()));
81
82 (arr1, arr2)
83}
84
85/// A heapless version of `bench_swap_setup`.
86fn _bench_swap_setup_heapless<T, N>() -> (heapless::Vec<T, N>, heapless::Vec<T, N>)
87where
88 N: heapless::ArrayLength<T>,
89 T: Num + Clone,
90{
91 let mut arr1: heapless::Vec<T, N> = heapless::Vec::new();
92 let mut arr2 = arr1.clone();
93
94 arr1.resize(N::to_usize(), black_box(zero()))
95 .expect("insufficient stack space");
96 arr2.resize(N::to_usize(), black_box(one()))
97 .expect("insufficient stack space");
98
99 (arr1, arr2)
100}
101
102type BenchInt = u8;
103
104#[bench]
105fn bench_swap_loop_len_10(b: &mut Bencher) {
106 bench_swap_loop::<BenchInt, U10>(b);
107}
108
109#[bench]
110fn bench_swap_loop_len_100(b: &mut Bencher) {
111 bench_swap_loop::<BenchInt, U100>(b);
112}
113
114#[bench]
115fn bench_swap_loop_len_1000(b: &mut Bencher) {
116 bench_swap_loop::<BenchInt, U1000>(b);
117}
118
119#[bench]
120fn bench_swap_loop_len_10000(b: &mut Bencher) {
121 bench_swap_loop::<BenchInt, U10000>(b);
122}
123
124#[bench]
125fn bench_swap_loop_len_100000(b: &mut Bencher) {
126 bench_swap_loop::<BenchInt, U100000>(b);
127}
128
129#[bench]
130fn bench_swap_loop_len_1000000(b: &mut Bencher) {
131 bench_swap_loop::<BenchInt, U1000000>(b);
132}
133
134#[bench]
135fn bench_swap_ptrswap_len_10(b: &mut Bencher) {
136 bench_swap_ptrswap::<BenchInt, U10>(b);
137}
138
139#[bench]
140fn bench_swap_ptrswap_len_100(b: &mut Bencher) {
141 bench_swap_ptrswap::<BenchInt, U100>(b);
142}
143
144#[bench]
145fn bench_swap_ptrswap_len_1000(b: &mut Bencher) {
146 bench_swap_ptrswap::<BenchInt, U1000>(b);
147}
148
149#[bench]
150fn bench_swap_ptrswap_len_10000(b: &mut Bencher) {
151 bench_swap_ptrswap::<BenchInt, U10000>(b);
152}
153
154#[bench]
155fn bench_swap_ptrswap_len_100000(b: &mut Bencher) {
156 bench_swap_ptrswap::<BenchInt, U100000>(b);
157}
158
159#[bench]
160fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) {
161 bench_swap_ptrswap::<BenchInt, U1000000>(b);
162}
163
164#[bench]
165fn bench_cswap_loop_len_10(b: &mut Bencher) {
166 bench_cswap_loop::<BenchInt, U10>(b);
167}
168
169#[bench]
170fn bench_cswap_loop_len_100(b: &mut Bencher) {
171 bench_cswap_loop::<BenchInt, U100>(b);
172}
173
174#[bench]
175fn bench_cswap_loop_len_1000(b: &mut Bencher) {
176 bench_cswap_loop::<BenchInt, U1000>(b);
177}
178
179#[bench]
180fn bench_cswap_loop_len_10000(b: &mut Bencher) {
181 bench_cswap_loop::<BenchInt, U10000>(b);
182}
183
184#[bench]
185fn bench_cswap_loop_len_100000(b: &mut Bencher) {
186 bench_cswap_loop::<BenchInt, U100000>(b);
187}
188
189#[bench]
190fn bench_cswap_loop_len_1000000(b: &mut Bencher) {
191 bench_cswap_loop::<BenchInt, U1000000>(b);
192}
193
194#[bench]
195fn bench_cswap_alloca_len_10(b: &mut Bencher) {
196 bench_cswap_alloca::<BenchInt, U10>(b);
197}
198
199#[bench]
200fn bench_cswap_alloca_len_100(b: &mut Bencher) {
201 bench_cswap_alloca::<BenchInt, U100>(b);
202}
203
204#[bench]
205fn bench_cswap_alloca_len_1000(b: &mut Bencher) {
206 bench_cswap_alloca::<BenchInt, U1000>(b);
207}
208
209#[bench]
210fn bench_cswap_alloca_len_10000(b: &mut Bencher) {
211 bench_cswap_alloca::<BenchInt, U10000>(b);
212}
213
214#[bench]
215fn bench_cswap_alloca_len_100000(b: &mut Bencher) {
216 bench_cswap_alloca::<BenchInt, U100000>(b);
217}
218
219#[bench]
220fn bench_cswap_alloca_len_1000000(b: &mut Bencher) {
221 bench_cswap_alloca::<BenchInt, U1000000>(b);
222}