nalgebra/benches/common/macros.rs

138 lines
3.7 KiB
Rust
Raw Permalink Normal View History

2015-01-08 04:11:09 +08:00
#![macro_use]
2014-11-07 23:15:56 +08:00
macro_rules! bench_binop(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
#[bench]
fn $name(bh: &mut Bencher) {
const LEN: usize = 1 << 13;
2014-11-07 23:15:56 +08:00
let mut rng = IsaacRng::new_unseeded();
let elems1: Vec<$t1> = (0usize .. LEN).map(|_| rng.gen::<$t1>()).collect();
let elems2: Vec<$t2> = (0usize .. LEN).map(|_| rng.gen::<$t2>()).collect();
2014-11-07 23:15:56 +08:00
let mut i = 0;
bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
test::black_box(elems1.get_unchecked(i).$binop(*elems2.get_unchecked(i)))
}
})
}
}
);
macro_rules! bench_binop_ref(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
#[bench]
fn $name(bh: &mut Bencher) {
const LEN: usize = 1 << 13;
let mut rng = IsaacRng::new_unseeded();
let elems1: Vec<$t1> = (0usize .. LEN).map(|_| rng.gen::<$t1>()).collect();
let elems2: Vec<$t2> = (0usize .. LEN).map(|_| rng.gen::<$t2>()).collect();
let mut i = 0;
bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
test::black_box(elems1.get_unchecked(i).$binop(elems2.get_unchecked(i)))
2014-11-07 23:15:56 +08:00
}
})
}
}
2014-12-27 18:15:13 +08:00
);
2014-11-07 23:15:56 +08:00
macro_rules! bench_binop_na(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
#[bench]
fn $name(bh: &mut Bencher) {
const LEN: usize = 1 << 13;
2014-11-07 23:15:56 +08:00
let mut rng = IsaacRng::new_unseeded();
let elems1: Vec<$t1> = (0usize .. LEN).map(|_| rng.gen::<$t1>()).collect();
let elems2: Vec<$t2> = (0usize .. LEN).map(|_| rng.gen::<$t2>()).collect();
2014-11-07 23:15:56 +08:00
let mut i = 0;
bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
2015-01-05 22:12:06 +08:00
test::black_box(na::$binop(elems1.get_unchecked(i), elems2.get_unchecked(i)))
2014-11-07 23:15:56 +08:00
}
})
}
}
2014-12-27 18:15:13 +08:00
);
2014-11-07 23:15:56 +08:00
macro_rules! bench_unop_na(
2014-11-07 23:15:56 +08:00
($name: ident, $t: ty, $unop: ident) => {
#[bench]
fn $name(bh: &mut Bencher) {
const LEN: usize = 1 << 13;
2014-11-07 23:15:56 +08:00
let mut rng = IsaacRng::new_unseeded();
let elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect();
2014-11-07 23:15:56 +08:00
let mut i = 0;
bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
2015-01-05 22:12:06 +08:00
test::black_box(na::$unop(elems.get_unchecked(i)))
2014-11-07 23:15:56 +08:00
}
})
}
}
2014-12-27 18:15:13 +08:00
);
2014-11-07 23:15:56 +08:00
macro_rules! bench_unop(
2014-11-07 23:15:56 +08:00
($name: ident, $t: ty, $unop: ident) => {
#[bench]
fn $name(bh: &mut Bencher) {
const LEN: usize = 1 << 13;
2014-11-07 23:15:56 +08:00
let mut rng = IsaacRng::new_unseeded();
let mut elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect();
2014-11-07 23:15:56 +08:00
let mut i = 0;
bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
2015-01-02 06:23:35 +08:00
test::black_box(elems.get_unchecked_mut(i).$unop())
2014-11-07 23:15:56 +08:00
}
})
}
}
2014-12-27 18:15:13 +08:00
);
2014-11-07 23:15:56 +08:00
macro_rules! bench_construction(
2015-01-10 05:52:44 +08:00
($name: ident, $constructor: path, $( $args: ident: $types: ty),*) => {
2014-11-07 23:15:56 +08:00
#[bench]
fn $name(bh: &mut Bencher) {
const LEN: usize = 1 << 13;
2014-11-07 23:15:56 +08:00
let mut rng = IsaacRng::new_unseeded();
$(let $args: Vec<$types> = (0usize .. LEN).map(|_| rng.gen::<$types>()).collect();)*
2014-11-07 23:15:56 +08:00
let mut i = 0;
bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
2015-01-05 22:12:06 +08:00
let res = $constructor($(*$args.get_unchecked(i),)*);
2014-11-07 23:15:56 +08:00
test::black_box(res)
}
})
}
}
2014-12-27 18:15:13 +08:00
);