blob: 3f693693f8c40ea3a22ee69b4a5c0d8b0e432477 [file] [log] [blame]
#![cfg(test)]
#![feature(test)]
#![feature(slice_fill)]
extern crate num;
extern crate test;
use rust_samples::{cswap, swap};
use heapless::consts::{U10, U100, U1000, U10000, U100000, U1000000};
use num::{one, zero, Num};
use test::{black_box, Bencher};
fn bench_swap_loop<T, N>(b: &mut Bencher)
where
N: heapless::ArrayLength<T>,
T: Num + Copy,
{
let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
b.iter(|| {
swap::swap_loop(&mut arr1, &mut arr2);
black_box(&arr1);
black_box(&arr2);
});
}
fn bench_swap_ptrswap<T, N>(b: &mut Bencher)
where
N: heapless::ArrayLength<T>,
T: Num + Copy,
{
let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
b.iter(|| {
swap::swap_ptrswap(&mut arr1, &mut arr2);
black_box(&arr1);
black_box(&arr2);
});
}
fn bench_cswap_loop<T, N>(b: &mut Bencher)
where
N: heapless::ArrayLength<T>,
T: Num + Copy,
{
let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
b.iter(|| {
cswap::cswap_loop(&mut arr1, &mut arr2);
black_box(&arr1);
black_box(&arr2);
});
}
fn bench_cswap_alloca<T, N>(b: &mut Bencher)
where
N: heapless::ArrayLength<T>,
T: Num + Copy,
{
let (mut arr1, mut arr2) = bench_swap_setup::<T, N>();
b.iter(|| {
cswap::cswap_alloca(&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;
#[bench]
fn bench_swap_loop_len_10(b: &mut Bencher) {
bench_swap_loop::<BenchInt, U10>(b);
}
#[bench]
fn bench_swap_loop_len_100(b: &mut Bencher) {
bench_swap_loop::<BenchInt, U100>(b);
}
#[bench]
fn bench_swap_loop_len_1000(b: &mut Bencher) {
bench_swap_loop::<BenchInt, U1000>(b);
}
#[bench]
fn bench_swap_loop_len_10000(b: &mut Bencher) {
bench_swap_loop::<BenchInt, U10000>(b);
}
#[bench]
fn bench_swap_loop_len_100000(b: &mut Bencher) {
bench_swap_loop::<BenchInt, U100000>(b);
}
#[bench]
fn bench_swap_loop_len_1000000(b: &mut Bencher) {
bench_swap_loop::<BenchInt, U1000000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_10(b: &mut Bencher) {
bench_swap_ptrswap::<BenchInt, U10>(b);
}
#[bench]
fn bench_swap_ptrswap_len_100(b: &mut Bencher) {
bench_swap_ptrswap::<BenchInt, U100>(b);
}
#[bench]
fn bench_swap_ptrswap_len_1000(b: &mut Bencher) {
bench_swap_ptrswap::<BenchInt, U1000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_10000(b: &mut Bencher) {
bench_swap_ptrswap::<BenchInt, U10000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_100000(b: &mut Bencher) {
bench_swap_ptrswap::<BenchInt, U100000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) {
bench_swap_ptrswap::<BenchInt, U1000000>(b);
}
#[bench]
fn bench_cswap_loop_len_10(b: &mut Bencher) {
bench_cswap_loop::<BenchInt, U10>(b);
}
#[bench]
fn bench_cswap_loop_len_100(b: &mut Bencher) {
bench_cswap_loop::<BenchInt, U100>(b);
}
#[bench]
fn bench_cswap_loop_len_1000(b: &mut Bencher) {
bench_cswap_loop::<BenchInt, U1000>(b);
}
#[bench]
fn bench_cswap_loop_len_10000(b: &mut Bencher) {
bench_cswap_loop::<BenchInt, U10000>(b);
}
#[bench]
fn bench_cswap_loop_len_100000(b: &mut Bencher) {
bench_cswap_loop::<BenchInt, U100000>(b);
}
#[bench]
fn bench_cswap_loop_len_1000000(b: &mut Bencher) {
bench_cswap_loop::<BenchInt, U1000000>(b);
}
#[bench]
fn bench_cswap_alloca_len_10(b: &mut Bencher) {
bench_cswap_alloca::<BenchInt, U10>(b);
}
#[bench]
fn bench_cswap_alloca_len_100(b: &mut Bencher) {
bench_cswap_alloca::<BenchInt, U100>(b);
}
#[bench]
fn bench_cswap_alloca_len_1000(b: &mut Bencher) {
bench_cswap_alloca::<BenchInt, U1000>(b);
}
#[bench]
fn bench_cswap_alloca_len_10000(b: &mut Bencher) {
bench_cswap_alloca::<BenchInt, U10000>(b);
}
#[bench]
fn bench_cswap_alloca_len_100000(b: &mut Bencher) {
bench_cswap_alloca::<BenchInt, U100000>(b);
}
#[bench]
fn bench_cswap_alloca_len_1000000(b: &mut Bencher) {
bench_cswap_alloca::<BenchInt, U1000000>(b);
}