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 | |
| 8 | use rust_samples::{cswap, swap}; |
| 9 | |
| 10 | use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000}; |
| 11 | use num::{one, zero, Num}; |
| 12 | use test::{black_box, Bencher}; |
| 13 | |
| 14 | fn bench_swap_loop<T, N>(b: &mut Bencher) |
| 15 | where |
| 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 | |
| 28 | fn bench_swap_ptrswap<T, N>(b: &mut Bencher) |
| 29 | where |
| 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 | |
| 42 | fn bench_cswap_loop<T, N>(b: &mut Bencher) |
| 43 | where |
| 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 | |
| 56 | fn bench_cswap_alloca<T, N>(b: &mut Bencher) |
| 57 | where |
| 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. |
| 71 | fn bench_swap_setup<T, N>() -> (Vec<T>, Vec<T>) |
| 72 | where |
| 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`. |
| 86 | fn _bench_swap_setup_heapless<T, N>() -> (heapless::Vec<T, N>, heapless::Vec<T, N>) |
| 87 | where |
| 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 | |
| 102 | type BenchInt = u8; |
| 103 | |
| 104 | #[bench] |
| 105 | fn bench_swap_loop_len_10(b: &mut Bencher) { |
| 106 | bench_swap_loop::<BenchInt, U10>(b); |
| 107 | } |
| 108 | |
| 109 | #[bench] |
| 110 | fn bench_swap_loop_len_100(b: &mut Bencher) { |
| 111 | bench_swap_loop::<BenchInt, U100>(b); |
| 112 | } |
| 113 | |
| 114 | #[bench] |
| 115 | fn bench_swap_loop_len_1000(b: &mut Bencher) { |
| 116 | bench_swap_loop::<BenchInt, U1000>(b); |
| 117 | } |
| 118 | |
| 119 | #[bench] |
| 120 | fn bench_swap_loop_len_10000(b: &mut Bencher) { |
| 121 | bench_swap_loop::<BenchInt, U10000>(b); |
| 122 | } |
| 123 | |
| 124 | #[bench] |
| 125 | fn bench_swap_loop_len_100000(b: &mut Bencher) { |
| 126 | bench_swap_loop::<BenchInt, U100000>(b); |
| 127 | } |
| 128 | |
| 129 | #[bench] |
| 130 | fn bench_swap_loop_len_1000000(b: &mut Bencher) { |
| 131 | bench_swap_loop::<BenchInt, U1000000>(b); |
| 132 | } |
| 133 | |
| 134 | #[bench] |
| 135 | fn bench_swap_ptrswap_len_10(b: &mut Bencher) { |
| 136 | bench_swap_ptrswap::<BenchInt, U10>(b); |
| 137 | } |
| 138 | |
| 139 | #[bench] |
| 140 | fn bench_swap_ptrswap_len_100(b: &mut Bencher) { |
| 141 | bench_swap_ptrswap::<BenchInt, U100>(b); |
| 142 | } |
| 143 | |
| 144 | #[bench] |
| 145 | fn bench_swap_ptrswap_len_1000(b: &mut Bencher) { |
| 146 | bench_swap_ptrswap::<BenchInt, U1000>(b); |
| 147 | } |
| 148 | |
| 149 | #[bench] |
| 150 | fn bench_swap_ptrswap_len_10000(b: &mut Bencher) { |
| 151 | bench_swap_ptrswap::<BenchInt, U10000>(b); |
| 152 | } |
| 153 | |
| 154 | #[bench] |
| 155 | fn bench_swap_ptrswap_len_100000(b: &mut Bencher) { |
| 156 | bench_swap_ptrswap::<BenchInt, U100000>(b); |
| 157 | } |
| 158 | |
| 159 | #[bench] |
| 160 | fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) { |
| 161 | bench_swap_ptrswap::<BenchInt, U1000000>(b); |
| 162 | } |
| 163 | |
| 164 | #[bench] |
| 165 | fn bench_cswap_loop_len_10(b: &mut Bencher) { |
| 166 | bench_cswap_loop::<BenchInt, U10>(b); |
| 167 | } |
| 168 | |
| 169 | #[bench] |
| 170 | fn bench_cswap_loop_len_100(b: &mut Bencher) { |
| 171 | bench_cswap_loop::<BenchInt, U100>(b); |
| 172 | } |
| 173 | |
| 174 | #[bench] |
| 175 | fn bench_cswap_loop_len_1000(b: &mut Bencher) { |
| 176 | bench_cswap_loop::<BenchInt, U1000>(b); |
| 177 | } |
| 178 | |
| 179 | #[bench] |
| 180 | fn bench_cswap_loop_len_10000(b: &mut Bencher) { |
| 181 | bench_cswap_loop::<BenchInt, U10000>(b); |
| 182 | } |
| 183 | |
| 184 | #[bench] |
| 185 | fn bench_cswap_loop_len_100000(b: &mut Bencher) { |
| 186 | bench_cswap_loop::<BenchInt, U100000>(b); |
| 187 | } |
| 188 | |
| 189 | #[bench] |
| 190 | fn bench_cswap_loop_len_1000000(b: &mut Bencher) { |
| 191 | bench_cswap_loop::<BenchInt, U1000000>(b); |
| 192 | } |
| 193 | |
| 194 | #[bench] |
| 195 | fn bench_cswap_alloca_len_10(b: &mut Bencher) { |
| 196 | bench_cswap_alloca::<BenchInt, U10>(b); |
| 197 | } |
| 198 | |
| 199 | #[bench] |
| 200 | fn bench_cswap_alloca_len_100(b: &mut Bencher) { |
| 201 | bench_cswap_alloca::<BenchInt, U100>(b); |
| 202 | } |
| 203 | |
| 204 | #[bench] |
| 205 | fn bench_cswap_alloca_len_1000(b: &mut Bencher) { |
| 206 | bench_cswap_alloca::<BenchInt, U1000>(b); |
| 207 | } |
| 208 | |
| 209 | #[bench] |
| 210 | fn bench_cswap_alloca_len_10000(b: &mut Bencher) { |
| 211 | bench_cswap_alloca::<BenchInt, U10000>(b); |
| 212 | } |
| 213 | |
| 214 | #[bench] |
| 215 | fn bench_cswap_alloca_len_100000(b: &mut Bencher) { |
| 216 | bench_cswap_alloca::<BenchInt, U100000>(b); |
| 217 | } |
| 218 | |
| 219 | #[bench] |
| 220 | fn bench_cswap_alloca_len_1000000(b: &mut Bencher) { |
| 221 | bench_cswap_alloca::<BenchInt, U1000000>(b); |
| 222 | } |