Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 1 | #![cfg(test)] |
| 2 | #![feature(test)] |
| 3 | #![feature(slice_fill)] |
| 4 | |
| 5 | extern crate num; |
| 6 | extern crate test; |
| 7 | |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 8 | use rust_samples::swapper::{CAllocaSwapper, CLoopSwapper, LoopSwapper, PtrSwapper, Swapper}; |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 9 | |
| 10 | use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000}; |
| 11 | use num::{one, zero, Num}; |
| 12 | use test::{black_box, Bencher}; |
| 13 | |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 14 | fn bench_swap<S, T, N>(b: &mut Bencher) |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 15 | where |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 16 | S: Swapper, |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 17 | 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 Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 23 | S::swap(&mut arr1, &mut arr2); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 24 | black_box(&arr1); |
| 25 | black_box(&arr2); |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | /// Allocates two vectors of fixed length on the heap. |
| 30 | fn bench_swap_setup<T, N>() -> (Vec<T>, Vec<T>) |
| 31 | where |
| 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`. |
| 45 | fn _bench_swap_setup_heapless<T, N>() -> (heapless::Vec<T, N>, heapless::Vec<T, N>) |
| 46 | where |
| 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 | |
| 61 | type BenchInt = u8; |
| 62 | |
| 63 | #[bench] |
| 64 | fn bench_swap_loop_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 65 | bench_swap::<LoopSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | #[bench] |
| 69 | fn bench_swap_loop_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 70 | bench_swap::<LoopSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | #[bench] |
| 74 | fn bench_swap_loop_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 75 | bench_swap::<LoopSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | #[bench] |
| 79 | fn bench_swap_loop_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 80 | bench_swap::<LoopSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #[bench] |
| 84 | fn bench_swap_loop_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 85 | bench_swap::<LoopSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | #[bench] |
| 89 | fn bench_swap_loop_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 90 | bench_swap::<LoopSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | #[bench] |
| 94 | fn bench_swap_ptrswap_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 95 | bench_swap::<PtrSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #[bench] |
| 99 | fn bench_swap_ptrswap_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 100 | bench_swap::<PtrSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #[bench] |
| 104 | fn bench_swap_ptrswap_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 105 | bench_swap::<PtrSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | #[bench] |
| 109 | fn bench_swap_ptrswap_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 110 | bench_swap::<PtrSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | #[bench] |
| 114 | fn bench_swap_ptrswap_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 115 | bench_swap::<PtrSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #[bench] |
| 119 | fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 120 | bench_swap::<PtrSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | #[bench] |
| 124 | fn bench_cswap_loop_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 125 | bench_swap::<CLoopSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | #[bench] |
| 129 | fn bench_cswap_loop_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 130 | bench_swap::<CLoopSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #[bench] |
| 134 | fn bench_cswap_loop_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 135 | bench_swap::<CLoopSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | #[bench] |
| 139 | fn bench_cswap_loop_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 140 | bench_swap::<CLoopSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | #[bench] |
| 144 | fn bench_cswap_loop_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 145 | bench_swap::<CLoopSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #[bench] |
| 149 | fn bench_cswap_loop_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 150 | bench_swap::<CLoopSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | #[bench] |
| 154 | fn bench_cswap_alloca_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 155 | bench_swap::<CAllocaSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | #[bench] |
| 159 | fn bench_cswap_alloca_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 160 | bench_swap::<CAllocaSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | #[bench] |
| 164 | fn bench_cswap_alloca_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 165 | bench_swap::<CAllocaSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | #[bench] |
| 169 | fn bench_cswap_alloca_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 170 | bench_swap::<CAllocaSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | #[bench] |
| 174 | fn bench_cswap_alloca_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 175 | bench_swap::<CAllocaSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | #[bench] |
| 179 | fn bench_cswap_alloca_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame^] | 180 | bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 181 | } |