Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 1 | #![cfg(test)] |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 2 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 3 | extern crate criterion; |
| 4 | extern crate criterion_bencher_compat; |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 5 | extern crate num; |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 6 | |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 7 | use rust_samples::swapper::{ |
| 8 | CAllocaSwapper, CLoopSwapper, CMallocSwapper, LoopSwapper, PtrSwapper, Swapper, |
| 9 | }; |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 10 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 11 | use criterion::measurement::WallTime; |
| 12 | use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion}; |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 13 | use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000}; |
| 14 | use num::{one, zero, Num}; |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 15 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 16 | fn bench_swap<S, T, N>(b: &mut Bencher<WallTime>) |
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 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 65 | fn bench_swap_loop_len_10(c: &mut Criterion) { |
| 66 | c.bench_function("swap_loop_len_10", |b| { |
| 67 | bench_swap::<LoopSwapper, BenchInt, U10>(b) |
| 68 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 71 | fn bench_swap_loop_len_100(c: &mut Criterion) { |
| 72 | c.bench_function("swap_loop_len_100", |b| { |
| 73 | bench_swap::<LoopSwapper, BenchInt, U100>(b) |
| 74 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 77 | fn bench_swap_loop_len_1000(c: &mut Criterion) { |
| 78 | c.bench_function("swap_loop_len_1000", |b| { |
| 79 | bench_swap::<LoopSwapper, BenchInt, U1000>(b) |
| 80 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 83 | fn bench_swap_loop_len_10000(c: &mut Criterion) { |
| 84 | c.bench_function("swap_loop_len_10000", |b| { |
| 85 | bench_swap::<LoopSwapper, BenchInt, U10000>(b) |
| 86 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 89 | fn bench_swap_loop_len_100000(c: &mut Criterion) { |
| 90 | c.bench_function("swap_loop_len_100000", |b| { |
| 91 | bench_swap::<LoopSwapper, BenchInt, U100000>(b) |
| 92 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 95 | fn bench_swap_loop_len_1000000(c: &mut Criterion) { |
| 96 | c.bench_function("swap_loop_len_1000000", |b| { |
| 97 | bench_swap::<LoopSwapper, BenchInt, U1000000>(b) |
| 98 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 101 | fn bench_swap_ptrswap_len_10(c: &mut Criterion) { |
| 102 | c.bench_function("swap_ptrswap_len_10", |b| { |
| 103 | bench_swap::<PtrSwapper, BenchInt, U10>(b) |
| 104 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 107 | fn bench_swap_ptrswap_len_100(c: &mut Criterion) { |
| 108 | c.bench_function("swap_ptrswap_len_100", |b| { |
| 109 | bench_swap::<PtrSwapper, BenchInt, U100>(b) |
| 110 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 113 | fn bench_swap_ptrswap_len_1000(c: &mut Criterion) { |
| 114 | c.bench_function("swap_ptrswap_len_1000", |b| { |
| 115 | bench_swap::<PtrSwapper, BenchInt, U1000>(b) |
| 116 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 119 | fn bench_swap_ptrswap_len_10000(c: &mut Criterion) { |
| 120 | c.bench_function("swap_ptrswap_len_10000", |b| { |
| 121 | bench_swap::<PtrSwapper, BenchInt, U10000>(b) |
| 122 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 125 | fn bench_swap_ptrswap_len_100000(c: &mut Criterion) { |
| 126 | c.bench_function("swap_ptrswap_len_100000", |b| { |
| 127 | bench_swap::<PtrSwapper, BenchInt, U100000>(b) |
| 128 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 131 | fn bench_swap_ptrswap_len_1000000(c: &mut Criterion) { |
| 132 | c.bench_function("swap_ptrswap_len_1000000", |b| { |
| 133 | bench_swap::<PtrSwapper, BenchInt, U1000000>(b) |
| 134 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 137 | fn bench_cswap_loop_len_10(c: &mut Criterion) { |
| 138 | c.bench_function("cswap_loop_len_10", |b| { |
| 139 | bench_swap::<CLoopSwapper, BenchInt, U10>(b) |
| 140 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 143 | fn bench_cswap_loop_len_100(c: &mut Criterion) { |
| 144 | c.bench_function("cswap_loop_len_100", |b| { |
| 145 | bench_swap::<CLoopSwapper, BenchInt, U100>(b) |
| 146 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 149 | fn bench_cswap_loop_len_1000(c: &mut Criterion) { |
| 150 | c.bench_function("cswap_loop_len_1000", |b| { |
| 151 | bench_swap::<CLoopSwapper, BenchInt, U1000>(b) |
| 152 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 155 | fn bench_cswap_loop_len_10000(c: &mut Criterion) { |
| 156 | c.bench_function("cswap_loop_len_10000", |b| { |
| 157 | bench_swap::<CLoopSwapper, BenchInt, U10000>(b) |
| 158 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 161 | fn bench_cswap_loop_len_100000(c: &mut Criterion) { |
| 162 | c.bench_function("cswap_loop_len_100000", |b| { |
| 163 | bench_swap::<CLoopSwapper, BenchInt, U100000>(b) |
| 164 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 167 | fn bench_cswap_loop_len_1000000(c: &mut Criterion) { |
| 168 | c.bench_function("cswap_loop_len_1000000", |b| { |
| 169 | bench_swap::<CLoopSwapper, BenchInt, U1000000>(b) |
| 170 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 173 | fn bench_cswap_alloca_len_10(c: &mut Criterion) { |
| 174 | c.bench_function("cswap_alloca_len_10", |b| { |
| 175 | bench_swap::<CAllocaSwapper, BenchInt, U10>(b) |
| 176 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 179 | fn bench_cswap_alloca_len_100(c: &mut Criterion) { |
| 180 | c.bench_function("cswap_alloca_len_100", |b| { |
| 181 | bench_swap::<CAllocaSwapper, BenchInt, U100>(b) |
| 182 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 185 | fn bench_cswap_alloca_len_1000(c: &mut Criterion) { |
| 186 | c.bench_function("cswap_alloca_len_1000", |b| { |
| 187 | bench_swap::<CAllocaSwapper, BenchInt, U1000>(b) |
| 188 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 191 | fn bench_cswap_alloca_len_10000(c: &mut Criterion) { |
| 192 | c.bench_function("cswap_alloca_len_10000", |b| { |
| 193 | bench_swap::<CAllocaSwapper, BenchInt, U10000>(b) |
| 194 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 197 | fn bench_cswap_alloca_len_100000(c: &mut Criterion) { |
| 198 | c.bench_function("cswap_alloca_len_100000", |b| { |
| 199 | bench_swap::<CAllocaSwapper, BenchInt, U100000>(b) |
| 200 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 203 | fn bench_cswap_alloca_len_1000000(c: &mut Criterion) { |
| 204 | c.bench_function("cswap_alloca_len_1000000", |b| { |
| 205 | bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b) |
| 206 | }); |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 207 | } |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 208 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 209 | fn bench_cswap_malloc_len_10(c: &mut Criterion) { |
| 210 | c.bench_function("cswap_malloc_len_10", |b| { |
| 211 | bench_swap::<CMallocSwapper, BenchInt, U10>(b) |
| 212 | }); |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 215 | fn bench_cswap_malloc_len_100(c: &mut Criterion) { |
| 216 | c.bench_function("cswap_malloc_len_100", |b| { |
| 217 | bench_swap::<CMallocSwapper, BenchInt, U100>(b) |
| 218 | }); |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 221 | fn bench_cswap_malloc_len_1000(c: &mut Criterion) { |
| 222 | c.bench_function("cswap_malloc_len_1000", |b| { |
| 223 | bench_swap::<CMallocSwapper, BenchInt, U1000>(b) |
| 224 | }); |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 227 | fn bench_cswap_malloc_len_10000(c: &mut Criterion) { |
| 228 | c.bench_function("cswap_malloc_len_10000", |b| { |
| 229 | bench_swap::<CMallocSwapper, BenchInt, U10000>(b) |
| 230 | }); |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 233 | fn bench_cswap_malloc_len_100000(c: &mut Criterion) { |
| 234 | c.bench_function("cswap_malloc_len_100000", |b| { |
| 235 | bench_swap::<CMallocSwapper, BenchInt, U100000>(b) |
| 236 | }); |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 239 | fn bench_cswap_malloc_len_1000000(c: &mut Criterion) { |
| 240 | c.bench_function("cswap_malloc_len_1000000", |b| { |
| 241 | bench_swap::<CMallocSwapper, BenchInt, U1000000>(b) |
| 242 | }); |
Matthias Andreas Benkard | 5032757 | 2020-06-30 22:53:44 +0200 | [diff] [blame] | 243 | } |
Matthias Andreas Benkard | 49de2bb | 2020-06-30 23:12:08 +0200 | [diff] [blame^] | 244 | |
| 245 | criterion_group!( |
| 246 | benches, |
| 247 | bench_swap_loop_len_10, |
| 248 | bench_swap_loop_len_100, |
| 249 | bench_swap_loop_len_1000, |
| 250 | bench_swap_loop_len_10000, |
| 251 | bench_swap_loop_len_100000, |
| 252 | bench_swap_loop_len_1000000, |
| 253 | bench_swap_ptrswap_len_10, |
| 254 | bench_swap_ptrswap_len_100, |
| 255 | bench_swap_ptrswap_len_1000, |
| 256 | bench_swap_ptrswap_len_10000, |
| 257 | bench_swap_ptrswap_len_100000, |
| 258 | bench_swap_ptrswap_len_1000000, |
| 259 | bench_cswap_loop_len_10, |
| 260 | bench_cswap_loop_len_100, |
| 261 | bench_cswap_loop_len_1000, |
| 262 | bench_cswap_loop_len_10000, |
| 263 | bench_cswap_loop_len_100000, |
| 264 | bench_cswap_loop_len_1000000, |
| 265 | bench_cswap_alloca_len_10, |
| 266 | bench_cswap_alloca_len_100, |
| 267 | bench_cswap_alloca_len_1000, |
| 268 | bench_cswap_alloca_len_10000, |
| 269 | bench_cswap_alloca_len_100000, |
| 270 | bench_cswap_alloca_len_1000000, |
| 271 | bench_cswap_malloc_len_10, |
| 272 | bench_cswap_malloc_len_100, |
| 273 | bench_cswap_malloc_len_1000, |
| 274 | bench_cswap_malloc_len_10000, |
| 275 | bench_cswap_malloc_len_100000, |
| 276 | bench_cswap_malloc_len_1000000 |
| 277 | ); |
| 278 | criterion_main!(benches); |