blob: 5d5b9581c637d0b139727ae0d46c8da0a9c2adef [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
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +02008use rust_samples::swapper::{CAllocaSwapper, CLoopSwapper, LoopSwapper, PtrSwapper, Swapper};
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02009
10use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000};
11use num::{one, zero, Num};
12use test::{black_box, Bencher};
13
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020014fn bench_swap<S, T, N>(b: &mut Bencher)
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020015where
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020016 S: Swapper,
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020017 N: heapless::ArrayLength<T>,
18 T: Num + Copy,
19{
20 let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
21
22 b.iter(|| {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020023 S::swap(&mut arr1, &mut arr2);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020024 black_box(&arr1);
25 black_box(&arr2);
26 });
27}
28
29/// Allocates two vectors of fixed length on the heap.
30fn bench_swap_setup<T, N>() -> (Vec<T>, Vec<T>)
31where
32 N: heapless::ArrayLength<T>,
33 T: Num + Clone,
34{
35 let mut arr1: Vec<T> = Vec::new();
36 let mut arr2 = arr1.clone();
37
38 arr1.resize(N::to_usize(), black_box(zero()));
39 arr2.resize(N::to_usize(), black_box(one()));
40
41 (arr1, arr2)
42}
43
44/// A heapless version of `bench_swap_setup`.
45fn _bench_swap_setup_heapless<T, N>() -> (heapless::Vec<T, N>, heapless::Vec<T, N>)
46where
47 N: heapless::ArrayLength<T>,
48 T: Num + Clone,
49{
50 let mut arr1: heapless::Vec<T, N> = heapless::Vec::new();
51 let mut arr2 = arr1.clone();
52
53 arr1.resize(N::to_usize(), black_box(zero()))
54 .expect("insufficient stack space");
55 arr2.resize(N::to_usize(), black_box(one()))
56 .expect("insufficient stack space");
57
58 (arr1, arr2)
59}
60
61type BenchInt = u8;
62
63#[bench]
64fn bench_swap_loop_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020065 bench_swap::<LoopSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020066}
67
68#[bench]
69fn bench_swap_loop_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020070 bench_swap::<LoopSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020071}
72
73#[bench]
74fn bench_swap_loop_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020075 bench_swap::<LoopSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020076}
77
78#[bench]
79fn bench_swap_loop_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020080 bench_swap::<LoopSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020081}
82
83#[bench]
84fn bench_swap_loop_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020085 bench_swap::<LoopSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020086}
87
88#[bench]
89fn bench_swap_loop_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020090 bench_swap::<LoopSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020091}
92
93#[bench]
94fn bench_swap_ptrswap_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020095 bench_swap::<PtrSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020096}
97
98#[bench]
99fn bench_swap_ptrswap_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200100 bench_swap::<PtrSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200101}
102
103#[bench]
104fn bench_swap_ptrswap_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200105 bench_swap::<PtrSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200106}
107
108#[bench]
109fn bench_swap_ptrswap_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200110 bench_swap::<PtrSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200111}
112
113#[bench]
114fn bench_swap_ptrswap_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200115 bench_swap::<PtrSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200116}
117
118#[bench]
119fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200120 bench_swap::<PtrSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200121}
122
123#[bench]
124fn bench_cswap_loop_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200125 bench_swap::<CLoopSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200126}
127
128#[bench]
129fn bench_cswap_loop_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200130 bench_swap::<CLoopSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200131}
132
133#[bench]
134fn bench_cswap_loop_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200135 bench_swap::<CLoopSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200136}
137
138#[bench]
139fn bench_cswap_loop_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200140 bench_swap::<CLoopSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200141}
142
143#[bench]
144fn bench_cswap_loop_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200145 bench_swap::<CLoopSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200146}
147
148#[bench]
149fn bench_cswap_loop_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200150 bench_swap::<CLoopSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200151}
152
153#[bench]
154fn bench_cswap_alloca_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200155 bench_swap::<CAllocaSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200156}
157
158#[bench]
159fn bench_cswap_alloca_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200160 bench_swap::<CAllocaSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200161}
162
163#[bench]
164fn bench_cswap_alloca_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200165 bench_swap::<CAllocaSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200166}
167
168#[bench]
169fn bench_cswap_alloca_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200170 bench_swap::<CAllocaSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200171}
172
173#[bench]
174fn bench_cswap_alloca_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200175 bench_swap::<CAllocaSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200176}
177
178#[bench]
179fn bench_cswap_alloca_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200180 bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200181}