Refactor swapper code with traits.
Change-Id: Ia27d3a11fea86d2ac430c3455d5247cfc4891e70
diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs
index 3f69369..5d5b958 100644
--- a/benches/benchmarks.rs
+++ b/benches/benchmarks.rs
@@ -5,63 +5,22 @@
extern crate num;
extern crate test;
-use rust_samples::{cswap, swap};
+use rust_samples::swapper::{CAllocaSwapper, CLoopSwapper, LoopSwapper, PtrSwapper, Swapper};
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)
+fn bench_swap<S, T, N>(b: &mut Bencher)
where
+ S: Swapper,
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);
+ S::swap(&mut arr1, &mut arr2);
black_box(&arr1);
black_box(&arr2);
});
@@ -103,120 +62,120 @@
#[bench]
fn bench_swap_loop_len_10(b: &mut Bencher) {
- bench_swap_loop::<BenchInt, U10>(b);
+ bench_swap::<LoopSwapper, BenchInt, U10>(b);
}
#[bench]
fn bench_swap_loop_len_100(b: &mut Bencher) {
- bench_swap_loop::<BenchInt, U100>(b);
+ bench_swap::<LoopSwapper, BenchInt, U100>(b);
}
#[bench]
fn bench_swap_loop_len_1000(b: &mut Bencher) {
- bench_swap_loop::<BenchInt, U1000>(b);
+ bench_swap::<LoopSwapper, BenchInt, U1000>(b);
}
#[bench]
fn bench_swap_loop_len_10000(b: &mut Bencher) {
- bench_swap_loop::<BenchInt, U10000>(b);
+ bench_swap::<LoopSwapper, BenchInt, U10000>(b);
}
#[bench]
fn bench_swap_loop_len_100000(b: &mut Bencher) {
- bench_swap_loop::<BenchInt, U100000>(b);
+ bench_swap::<LoopSwapper, BenchInt, U100000>(b);
}
#[bench]
fn bench_swap_loop_len_1000000(b: &mut Bencher) {
- bench_swap_loop::<BenchInt, U1000000>(b);
+ bench_swap::<LoopSwapper, BenchInt, U1000000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_10(b: &mut Bencher) {
- bench_swap_ptrswap::<BenchInt, U10>(b);
+ bench_swap::<PtrSwapper, BenchInt, U10>(b);
}
#[bench]
fn bench_swap_ptrswap_len_100(b: &mut Bencher) {
- bench_swap_ptrswap::<BenchInt, U100>(b);
+ bench_swap::<PtrSwapper, BenchInt, U100>(b);
}
#[bench]
fn bench_swap_ptrswap_len_1000(b: &mut Bencher) {
- bench_swap_ptrswap::<BenchInt, U1000>(b);
+ bench_swap::<PtrSwapper, BenchInt, U1000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_10000(b: &mut Bencher) {
- bench_swap_ptrswap::<BenchInt, U10000>(b);
+ bench_swap::<PtrSwapper, BenchInt, U10000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_100000(b: &mut Bencher) {
- bench_swap_ptrswap::<BenchInt, U100000>(b);
+ bench_swap::<PtrSwapper, BenchInt, U100000>(b);
}
#[bench]
fn bench_swap_ptrswap_len_1000000(b: &mut Bencher) {
- bench_swap_ptrswap::<BenchInt, U1000000>(b);
+ bench_swap::<PtrSwapper, BenchInt, U1000000>(b);
}
#[bench]
fn bench_cswap_loop_len_10(b: &mut Bencher) {
- bench_cswap_loop::<BenchInt, U10>(b);
+ bench_swap::<CLoopSwapper, BenchInt, U10>(b);
}
#[bench]
fn bench_cswap_loop_len_100(b: &mut Bencher) {
- bench_cswap_loop::<BenchInt, U100>(b);
+ bench_swap::<CLoopSwapper, BenchInt, U100>(b);
}
#[bench]
fn bench_cswap_loop_len_1000(b: &mut Bencher) {
- bench_cswap_loop::<BenchInt, U1000>(b);
+ bench_swap::<CLoopSwapper, BenchInt, U1000>(b);
}
#[bench]
fn bench_cswap_loop_len_10000(b: &mut Bencher) {
- bench_cswap_loop::<BenchInt, U10000>(b);
+ bench_swap::<CLoopSwapper, BenchInt, U10000>(b);
}
#[bench]
fn bench_cswap_loop_len_100000(b: &mut Bencher) {
- bench_cswap_loop::<BenchInt, U100000>(b);
+ bench_swap::<CLoopSwapper, BenchInt, U100000>(b);
}
#[bench]
fn bench_cswap_loop_len_1000000(b: &mut Bencher) {
- bench_cswap_loop::<BenchInt, U1000000>(b);
+ bench_swap::<CLoopSwapper, BenchInt, U1000000>(b);
}
#[bench]
fn bench_cswap_alloca_len_10(b: &mut Bencher) {
- bench_cswap_alloca::<BenchInt, U10>(b);
+ bench_swap::<CAllocaSwapper, BenchInt, U10>(b);
}
#[bench]
fn bench_cswap_alloca_len_100(b: &mut Bencher) {
- bench_cswap_alloca::<BenchInt, U100>(b);
+ bench_swap::<CAllocaSwapper, BenchInt, U100>(b);
}
#[bench]
fn bench_cswap_alloca_len_1000(b: &mut Bencher) {
- bench_cswap_alloca::<BenchInt, U1000>(b);
+ bench_swap::<CAllocaSwapper, BenchInt, U1000>(b);
}
#[bench]
fn bench_cswap_alloca_len_10000(b: &mut Bencher) {
- bench_cswap_alloca::<BenchInt, U10000>(b);
+ bench_swap::<CAllocaSwapper, BenchInt, U10000>(b);
}
#[bench]
fn bench_cswap_alloca_len_100000(b: &mut Bencher) {
- bench_cswap_alloca::<BenchInt, U100000>(b);
+ bench_swap::<CAllocaSwapper, BenchInt, U100000>(b);
}
#[bench]
fn bench_cswap_alloca_len_1000000(b: &mut Bencher) {
- bench_cswap_alloca::<BenchInt, U1000000>(b);
+ bench_swap::<CAllocaSwapper, BenchInt, U1000000>(b);
}