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 | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame^] | 8 | use rust_samples::swapper::{ |
| 9 | CAllocaSwapper, CLoopSwapper, CMallocSwapper, LoopSwapper, PtrSwapper, Swapper, |
| 10 | }; |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 11 | |
| 12 | use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000}; |
| 13 | use num::{one, zero, Num}; |
| 14 | use test::{black_box, Bencher}; |
| 15 | |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 16 | fn bench_swap<S, T, N>(b: &mut Bencher) |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 17 | where |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 18 | S: Swapper, |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 19 | 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 Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 25 | S::swap(&mut arr1, &mut arr2); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 26 | black_box(&arr1); |
| 27 | black_box(&arr2); |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | /// Allocates two vectors of fixed length on the heap. |
| 32 | fn bench_swap_setup<T, N>() -> (Vec<T>, Vec<T>) |
| 33 | where |
| 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`. |
| 47 | fn _bench_swap_setup_heapless<T, N>() -> (heapless::Vec<T, N>, heapless::Vec<T, N>) |
| 48 | where |
| 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 | |
| 63 | type BenchInt = u8; |
| 64 | |
| 65 | #[bench] |
| 66 | fn bench_swap_loop_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 67 | bench_swap::<LoopSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | #[bench] |
| 71 | fn bench_swap_loop_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 72 | bench_swap::<LoopSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | #[bench] |
| 76 | fn bench_swap_loop_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 77 | bench_swap::<LoopSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | #[bench] |
| 81 | fn bench_swap_loop_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 82 | bench_swap::<LoopSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #[bench] |
| 86 | fn bench_swap_loop_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 87 | bench_swap::<LoopSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | #[bench] |
| 91 | fn bench_swap_loop_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 92 | bench_swap::<LoopSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | #[bench] |
| 96 | fn bench_swap_ptrswap_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 97 | bench_swap::<PtrSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | #[bench] |
| 101 | fn bench_swap_ptrswap_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 102 | bench_swap::<PtrSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #[bench] |
| 106 | fn bench_swap_ptrswap_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 107 | bench_swap::<PtrSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | #[bench] |
| 111 | fn bench_swap_ptrswap_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 112 | bench_swap::<PtrSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #[bench] |
| 116 | fn bench_swap_ptrswap_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 117 | bench_swap::<PtrSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | #[bench] |
| 121 | fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 122 | bench_swap::<PtrSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | #[bench] |
| 126 | fn bench_cswap_loop_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 127 | bench_swap::<CLoopSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #[bench] |
| 131 | fn bench_cswap_loop_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 132 | bench_swap::<CLoopSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | #[bench] |
| 136 | fn bench_cswap_loop_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 137 | bench_swap::<CLoopSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #[bench] |
| 141 | fn bench_cswap_loop_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 142 | bench_swap::<CLoopSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #[bench] |
| 146 | fn bench_cswap_loop_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 147 | bench_swap::<CLoopSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | #[bench] |
| 151 | fn bench_cswap_loop_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 152 | bench_swap::<CLoopSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | #[bench] |
| 156 | fn bench_cswap_alloca_len_10(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 157 | bench_swap::<CAllocaSwapper, BenchInt, U10>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | #[bench] |
| 161 | fn bench_cswap_alloca_len_100(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 162 | bench_swap::<CAllocaSwapper, BenchInt, U100>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | #[bench] |
| 166 | fn bench_cswap_alloca_len_1000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 167 | bench_swap::<CAllocaSwapper, BenchInt, U1000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | #[bench] |
| 171 | fn bench_cswap_alloca_len_10000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 172 | bench_swap::<CAllocaSwapper, BenchInt, U10000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | #[bench] |
| 176 | fn bench_cswap_alloca_len_100000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 177 | bench_swap::<CAllocaSwapper, BenchInt, U100000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | #[bench] |
| 181 | fn bench_cswap_alloca_len_1000000(b: &mut Bencher) { |
Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 182 | bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 183 | } |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame^] | 184 | |
| 185 | #[bench] |
| 186 | fn bench_cswap_malloc_len_10(b: &mut Bencher) { |
| 187 | bench_swap::<CMallocSwapper, BenchInt, U10>(b); |
| 188 | } |
| 189 | |
| 190 | #[bench] |
| 191 | fn bench_cswap_malloc_len_100(b: &mut Bencher) { |
| 192 | bench_swap::<CMallocSwapper, BenchInt, U100>(b); |
| 193 | } |
| 194 | |
| 195 | #[bench] |
| 196 | fn bench_cswap_malloc_len_1000(b: &mut Bencher) { |
| 197 | bench_swap::<CMallocSwapper, BenchInt, U1000>(b); |
| 198 | } |
| 199 | |
| 200 | #[bench] |
| 201 | fn bench_cswap_malloc_len_10000(b: &mut Bencher) { |
| 202 | bench_swap::<CMallocSwapper, BenchInt, U10000>(b); |
| 203 | } |
| 204 | |
| 205 | #[bench] |
| 206 | fn bench_cswap_malloc_len_100000(b: &mut Bencher) { |
| 207 | bench_swap::<CMallocSwapper, BenchInt, U100000>(b); |
| 208 | } |
| 209 | |
| 210 | #[bench] |
| 211 | fn bench_cswap_malloc_len_1000000(b: &mut Bencher) { |
| 212 | bench_swap::<CMallocSwapper, BenchInt, U1000000>(b); |
| 213 | } |