blob: ec5e75cd33c465461c82dc6b48adcff202327b37 [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 Benkard50327572020-06-30 22:53:44 +02008use rust_samples::swapper::{
9 CAllocaSwapper, CLoopSwapper, CMallocSwapper, LoopSwapper, PtrSwapper, Swapper,
10};
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020011
12use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000};
13use num::{one, zero, Num};
14use test::{black_box, Bencher};
15
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020016fn bench_swap<S, T, N>(b: &mut Bencher)
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
65#[bench]
66fn bench_swap_loop_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020067 bench_swap::<LoopSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020068}
69
70#[bench]
71fn bench_swap_loop_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020072 bench_swap::<LoopSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020073}
74
75#[bench]
76fn bench_swap_loop_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020077 bench_swap::<LoopSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020078}
79
80#[bench]
81fn bench_swap_loop_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020082 bench_swap::<LoopSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020083}
84
85#[bench]
86fn bench_swap_loop_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020087 bench_swap::<LoopSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020088}
89
90#[bench]
91fn bench_swap_loop_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020092 bench_swap::<LoopSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020093}
94
95#[bench]
96fn bench_swap_ptrswap_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +020097 bench_swap::<PtrSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020098}
99
100#[bench]
101fn bench_swap_ptrswap_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200102 bench_swap::<PtrSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200103}
104
105#[bench]
106fn bench_swap_ptrswap_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200107 bench_swap::<PtrSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200108}
109
110#[bench]
111fn bench_swap_ptrswap_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200112 bench_swap::<PtrSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200113}
114
115#[bench]
116fn bench_swap_ptrswap_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200117 bench_swap::<PtrSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200118}
119
120#[bench]
121fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200122 bench_swap::<PtrSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200123}
124
125#[bench]
126fn bench_cswap_loop_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200127 bench_swap::<CLoopSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200128}
129
130#[bench]
131fn bench_cswap_loop_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200132 bench_swap::<CLoopSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200133}
134
135#[bench]
136fn bench_cswap_loop_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200137 bench_swap::<CLoopSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200138}
139
140#[bench]
141fn bench_cswap_loop_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200142 bench_swap::<CLoopSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200143}
144
145#[bench]
146fn bench_cswap_loop_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200147 bench_swap::<CLoopSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200148}
149
150#[bench]
151fn bench_cswap_loop_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200152 bench_swap::<CLoopSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200153}
154
155#[bench]
156fn bench_cswap_alloca_len_10(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200157 bench_swap::<CAllocaSwapper, BenchInt, U10>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200158}
159
160#[bench]
161fn bench_cswap_alloca_len_100(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200162 bench_swap::<CAllocaSwapper, BenchInt, U100>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200163}
164
165#[bench]
166fn bench_cswap_alloca_len_1000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200167 bench_swap::<CAllocaSwapper, BenchInt, U1000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200168}
169
170#[bench]
171fn bench_cswap_alloca_len_10000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200172 bench_swap::<CAllocaSwapper, BenchInt, U10000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200173}
174
175#[bench]
176fn bench_cswap_alloca_len_100000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200177 bench_swap::<CAllocaSwapper, BenchInt, U100000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200178}
179
180#[bench]
181fn bench_cswap_alloca_len_1000000(b: &mut Bencher) {
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +0200182 bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b);
Matthias Andreas Benkard08586462020-06-28 22:42:43 +0200183}
Matthias Andreas Benkard50327572020-06-30 22:53:44 +0200184
185#[bench]
186fn bench_cswap_malloc_len_10(b: &mut Bencher) {
187 bench_swap::<CMallocSwapper, BenchInt, U10>(b);
188}
189
190#[bench]
191fn bench_cswap_malloc_len_100(b: &mut Bencher) {
192 bench_swap::<CMallocSwapper, BenchInt, U100>(b);
193}
194
195#[bench]
196fn bench_cswap_malloc_len_1000(b: &mut Bencher) {
197 bench_swap::<CMallocSwapper, BenchInt, U1000>(b);
198}
199
200#[bench]
201fn bench_cswap_malloc_len_10000(b: &mut Bencher) {
202 bench_swap::<CMallocSwapper, BenchInt, U10000>(b);
203}
204
205#[bench]
206fn bench_cswap_malloc_len_100000(b: &mut Bencher) {
207 bench_swap::<CMallocSwapper, BenchInt, U100000>(b);
208}
209
210#[bench]
211fn bench_cswap_malloc_len_1000000(b: &mut Bencher) {
212 bench_swap::<CMallocSwapper, BenchInt, U1000000>(b);
213}