| #![cfg(test)] |
| |
| extern crate criterion; |
| extern crate criterion_bencher_compat; |
| extern crate num; |
| |
| use rust_samples::swapper::{ |
| CAllocaSwapper, CLoopSwapper, CMallocSwapper, LoopSwapper, PtrSwapper, Swapper, |
| }; |
| |
| use criterion::measurement::WallTime; |
| use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion}; |
| use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000}; |
| use num::{one, zero, Num}; |
| |
| fn bench_swap<S, T, N>(b: &mut Bencher<WallTime>) |
| where |
| S: Swapper, |
| N: heapless::ArrayLength<T>, |
| T: Num + Copy, |
| { |
| let (mut arr1, mut arr2) = bench_swap_setup::<T, N>(); |
| |
| b.iter(|| { |
| S::swap(&mut arr1, &mut arr2); |
| black_box(&arr1); |
| black_box(&arr2); |
| }); |
| } |
| |
| /// Allocates two vectors of fixed length on the heap. |
| fn bench_swap_setup<T, N>() -> (Vec<T>, Vec<T>) |
| where |
| N: heapless::ArrayLength<T>, |
| T: Num + Clone, |
| { |
| let mut arr1: Vec<T> = Vec::new(); |
| let mut arr2 = arr1.clone(); |
| |
| arr1.resize(N::to_usize(), black_box(zero())); |
| arr2.resize(N::to_usize(), black_box(one())); |
| |
| (arr1, arr2) |
| } |
| |
| /// A heapless version of `bench_swap_setup`. |
| fn _bench_swap_setup_heapless<T, N>() -> (heapless::Vec<T, N>, heapless::Vec<T, N>) |
| where |
| N: heapless::ArrayLength<T>, |
| T: Num + Clone, |
| { |
| let mut arr1: heapless::Vec<T, N> = heapless::Vec::new(); |
| let mut arr2 = arr1.clone(); |
| |
| arr1.resize(N::to_usize(), black_box(zero())) |
| .expect("insufficient stack space"); |
| arr2.resize(N::to_usize(), black_box(one())) |
| .expect("insufficient stack space"); |
| |
| (arr1, arr2) |
| } |
| |
| type BenchInt = u8; |
| |
| fn bench_swap_loop_len_10(c: &mut Criterion) { |
| c.bench_function("swap_loop_len_10", |b| { |
| bench_swap::<LoopSwapper, BenchInt, U10>(b) |
| }); |
| } |
| |
| fn bench_swap_loop_len_100(c: &mut Criterion) { |
| c.bench_function("swap_loop_len_100", |b| { |
| bench_swap::<LoopSwapper, BenchInt, U100>(b) |
| }); |
| } |
| |
| fn bench_swap_loop_len_1000(c: &mut Criterion) { |
| c.bench_function("swap_loop_len_1000", |b| { |
| bench_swap::<LoopSwapper, BenchInt, U1000>(b) |
| }); |
| } |
| |
| fn bench_swap_loop_len_10000(c: &mut Criterion) { |
| c.bench_function("swap_loop_len_10000", |b| { |
| bench_swap::<LoopSwapper, BenchInt, U10000>(b) |
| }); |
| } |
| |
| fn bench_swap_loop_len_100000(c: &mut Criterion) { |
| c.bench_function("swap_loop_len_100000", |b| { |
| bench_swap::<LoopSwapper, BenchInt, U100000>(b) |
| }); |
| } |
| |
| fn bench_swap_loop_len_1000000(c: &mut Criterion) { |
| c.bench_function("swap_loop_len_1000000", |b| { |
| bench_swap::<LoopSwapper, BenchInt, U1000000>(b) |
| }); |
| } |
| |
| fn bench_swap_ptrswap_len_10(c: &mut Criterion) { |
| c.bench_function("swap_ptrswap_len_10", |b| { |
| bench_swap::<PtrSwapper, BenchInt, U10>(b) |
| }); |
| } |
| |
| fn bench_swap_ptrswap_len_100(c: &mut Criterion) { |
| c.bench_function("swap_ptrswap_len_100", |b| { |
| bench_swap::<PtrSwapper, BenchInt, U100>(b) |
| }); |
| } |
| |
| fn bench_swap_ptrswap_len_1000(c: &mut Criterion) { |
| c.bench_function("swap_ptrswap_len_1000", |b| { |
| bench_swap::<PtrSwapper, BenchInt, U1000>(b) |
| }); |
| } |
| |
| fn bench_swap_ptrswap_len_10000(c: &mut Criterion) { |
| c.bench_function("swap_ptrswap_len_10000", |b| { |
| bench_swap::<PtrSwapper, BenchInt, U10000>(b) |
| }); |
| } |
| |
| fn bench_swap_ptrswap_len_100000(c: &mut Criterion) { |
| c.bench_function("swap_ptrswap_len_100000", |b| { |
| bench_swap::<PtrSwapper, BenchInt, U100000>(b) |
| }); |
| } |
| |
| fn bench_swap_ptrswap_len_1000000(c: &mut Criterion) { |
| c.bench_function("swap_ptrswap_len_1000000", |b| { |
| bench_swap::<PtrSwapper, BenchInt, U1000000>(b) |
| }); |
| } |
| |
| fn bench_cswap_loop_len_10(c: &mut Criterion) { |
| c.bench_function("cswap_loop_len_10", |b| { |
| bench_swap::<CLoopSwapper, BenchInt, U10>(b) |
| }); |
| } |
| |
| fn bench_cswap_loop_len_100(c: &mut Criterion) { |
| c.bench_function("cswap_loop_len_100", |b| { |
| bench_swap::<CLoopSwapper, BenchInt, U100>(b) |
| }); |
| } |
| |
| fn bench_cswap_loop_len_1000(c: &mut Criterion) { |
| c.bench_function("cswap_loop_len_1000", |b| { |
| bench_swap::<CLoopSwapper, BenchInt, U1000>(b) |
| }); |
| } |
| |
| fn bench_cswap_loop_len_10000(c: &mut Criterion) { |
| c.bench_function("cswap_loop_len_10000", |b| { |
| bench_swap::<CLoopSwapper, BenchInt, U10000>(b) |
| }); |
| } |
| |
| fn bench_cswap_loop_len_100000(c: &mut Criterion) { |
| c.bench_function("cswap_loop_len_100000", |b| { |
| bench_swap::<CLoopSwapper, BenchInt, U100000>(b) |
| }); |
| } |
| |
| fn bench_cswap_loop_len_1000000(c: &mut Criterion) { |
| c.bench_function("cswap_loop_len_1000000", |b| { |
| bench_swap::<CLoopSwapper, BenchInt, U1000000>(b) |
| }); |
| } |
| |
| fn bench_cswap_alloca_len_10(c: &mut Criterion) { |
| c.bench_function("cswap_alloca_len_10", |b| { |
| bench_swap::<CAllocaSwapper, BenchInt, U10>(b) |
| }); |
| } |
| |
| fn bench_cswap_alloca_len_100(c: &mut Criterion) { |
| c.bench_function("cswap_alloca_len_100", |b| { |
| bench_swap::<CAllocaSwapper, BenchInt, U100>(b) |
| }); |
| } |
| |
| fn bench_cswap_alloca_len_1000(c: &mut Criterion) { |
| c.bench_function("cswap_alloca_len_1000", |b| { |
| bench_swap::<CAllocaSwapper, BenchInt, U1000>(b) |
| }); |
| } |
| |
| fn bench_cswap_alloca_len_10000(c: &mut Criterion) { |
| c.bench_function("cswap_alloca_len_10000", |b| { |
| bench_swap::<CAllocaSwapper, BenchInt, U10000>(b) |
| }); |
| } |
| |
| fn bench_cswap_alloca_len_100000(c: &mut Criterion) { |
| c.bench_function("cswap_alloca_len_100000", |b| { |
| bench_swap::<CAllocaSwapper, BenchInt, U100000>(b) |
| }); |
| } |
| |
| fn bench_cswap_alloca_len_1000000(c: &mut Criterion) { |
| c.bench_function("cswap_alloca_len_1000000", |b| { |
| bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b) |
| }); |
| } |
| |
| fn bench_cswap_malloc_len_10(c: &mut Criterion) { |
| c.bench_function("cswap_malloc_len_10", |b| { |
| bench_swap::<CMallocSwapper, BenchInt, U10>(b) |
| }); |
| } |
| |
| fn bench_cswap_malloc_len_100(c: &mut Criterion) { |
| c.bench_function("cswap_malloc_len_100", |b| { |
| bench_swap::<CMallocSwapper, BenchInt, U100>(b) |
| }); |
| } |
| |
| fn bench_cswap_malloc_len_1000(c: &mut Criterion) { |
| c.bench_function("cswap_malloc_len_1000", |b| { |
| bench_swap::<CMallocSwapper, BenchInt, U1000>(b) |
| }); |
| } |
| |
| fn bench_cswap_malloc_len_10000(c: &mut Criterion) { |
| c.bench_function("cswap_malloc_len_10000", |b| { |
| bench_swap::<CMallocSwapper, BenchInt, U10000>(b) |
| }); |
| } |
| |
| fn bench_cswap_malloc_len_100000(c: &mut Criterion) { |
| c.bench_function("cswap_malloc_len_100000", |b| { |
| bench_swap::<CMallocSwapper, BenchInt, U100000>(b) |
| }); |
| } |
| |
| fn bench_cswap_malloc_len_1000000(c: &mut Criterion) { |
| c.bench_function("cswap_malloc_len_1000000", |b| { |
| bench_swap::<CMallocSwapper, BenchInt, U1000000>(b) |
| }); |
| } |
| |
| criterion_group!( |
| benches, |
| bench_swap_loop_len_10, |
| bench_swap_loop_len_100, |
| bench_swap_loop_len_1000, |
| bench_swap_loop_len_10000, |
| bench_swap_loop_len_100000, |
| bench_swap_loop_len_1000000, |
| bench_swap_ptrswap_len_10, |
| bench_swap_ptrswap_len_100, |
| bench_swap_ptrswap_len_1000, |
| bench_swap_ptrswap_len_10000, |
| bench_swap_ptrswap_len_100000, |
| bench_swap_ptrswap_len_1000000, |
| bench_cswap_loop_len_10, |
| bench_cswap_loop_len_100, |
| bench_cswap_loop_len_1000, |
| bench_cswap_loop_len_10000, |
| bench_cswap_loop_len_100000, |
| bench_cswap_loop_len_1000000, |
| bench_cswap_alloca_len_10, |
| bench_cswap_alloca_len_100, |
| bench_cswap_alloca_len_1000, |
| bench_cswap_alloca_len_10000, |
| bench_cswap_alloca_len_100000, |
| bench_cswap_alloca_len_1000000, |
| bench_cswap_malloc_len_10, |
| bench_cswap_malloc_len_100, |
| bench_cswap_malloc_len_1000, |
| bench_cswap_malloc_len_10000, |
| bench_cswap_malloc_len_100000, |
| bench_cswap_malloc_len_1000000 |
| ); |
| criterion_main!(benches); |