Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ encase = { version = "0.12", optional = true }
serde_json = "1.0"
rand_xorshift = "0.4"
rand_isaac = "0.4"
criterion = { version = "0.4", features = ["html_reports"] }
criterion = { version = "0.7", features = ["html_reports"] }
nalgebra = { path = ".", features = ["debug", "compare", "rand", "macros"] }

# For matrix comparison macro
Expand Down Expand Up @@ -177,6 +177,7 @@ required-features = ["rand"]

[profile.bench]
lto = true
codegen-units = 1

[package.metadata.docs.rs]
# Enable all the features when building the docs on docs.rs
Expand Down
122 changes: 58 additions & 64 deletions benches/common/macros.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
#![macro_use]

/// This will call `Drop::drop()` within the benchmarking loop for all arguments that were not consumed
/// by the binary operation.
///
/// Do not use this macro for types with non-trivial `Drop` implementation unless you want to include it
/// into the measurement.
macro_rules! bench_binop(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
use rand::SeedableRng;

let mut rng = IsaacRng::seed_from_u64(0);
let a = rng.random::<$t1>();
let b = rng.random::<$t2>();

bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
a.$binop(b)
}));
bh.bench_function(stringify!($name), |bh| bh.iter_batched(
|| (rng.random::<$t1>(), rng.random::<$t2>()),
|args| {
args.0.$binop(args.1)
},
criterion::BatchSize::SmallInput),
);
}
}
);
Expand All @@ -19,95 +27,81 @@ macro_rules! bench_binop_ref(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
use rand::SeedableRng;

let mut rng = IsaacRng::seed_from_u64(0);
let a = rng.random::<$t1>();
let b = rng.random::<$t2>();

bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
a.$binop(&b)
}));
bh.bench_function(stringify!($name), |bh| bh.iter_batched_ref(
|| (rng.random::<$t1>(), rng.random::<$t2>()),
|args| {
args.0.$binop(&args.1)
},
criterion::BatchSize::SmallInput),
);
}
}
);

macro_rules! bench_binop_fn(
($name: ident, $t1: ty, $t2: ty, $binop: path) => {
/// This will call `Drop::drop()` within the benchmarking loop for all arguments that were not consumed
/// by the binary operation.
///
/// Do not use this macro for types with non-trivial `Drop` implementation unless you want to include it
/// into the measurement.
macro_rules! bench_binop_single_1st(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
use rand::SeedableRng;
use std::hint::black_box;

let mut rng = IsaacRng::seed_from_u64(0);
let a = rng.random::<$t1>();
let b = rng.random::<$t2>();

bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
$binop(&a, &b)
}));
let first = black_box(rng.random::<$t1>());

bh.bench_function(stringify!($name), |bh| bh.iter_batched(
|| rng.random::<$t2>(),
|second| {
first.$binop(second)
},
criterion::BatchSize::SmallInput),
);
}
}
);

macro_rules! bench_unop_na(
($name: ident, $t: ty, $unop: ident) => {
macro_rules! bench_binop_single_1st_ref(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
const LEN: usize = 1 << 13;

use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);
use std::hint::black_box;

let elems: Vec<$t> = (0usize .. LEN).map(|_| rng.random::<$t>()).collect();
let mut i = 0;
let mut rng = IsaacRng::seed_from_u64(0);

bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
i = (i + 1) & (LEN - 1);
let first = black_box(rng.random::<$t1>());

unsafe {
std::hint::black_box(na::$unop(elems.get_unchecked(i)))
}
}));
bh.bench_function(stringify!($name), |bh| bh.iter_batched_ref(
|| rng.random::<$t2>(),
|second| {
first.$binop(second)
},
criterion::BatchSize::SmallInput),
);
}
}
);

macro_rules! bench_unop(
($name: ident, $t: ty, $unop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
const LEN: usize = 1 << 13;

use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);

let mut elems: Vec<$t> = (0usize .. LEN).map(|_| rng.random::<$t>()).collect();
let mut i = 0;

bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
i = (i + 1) & (LEN - 1);

unsafe {
std::hint::black_box(elems.get_unchecked_mut(i).$unop())
}
}));
}
}
);

macro_rules! bench_construction(
($name: ident, $constructor: path, $( $args: ident: $types: ty),*) => {
fn $name(bh: &mut criterion::Criterion) {
const LEN: usize = 1 << 13;

use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);

$(let $args: Vec<$types> = (0usize .. LEN).map(|_| rng.random::<$types>()).collect();)*
let mut i = 0;

bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
i = (i + 1) & (LEN - 1);

unsafe {
let res = $constructor($(*$args.get_unchecked(i),)*);
std::hint::black_box(res)
}
}));
bh.bench_function(stringify!($name), |bh| bh.iter_batched_ref(
|| rng.random::<$t>(),
|arg| {
arg.$unop()
},
criterion::BatchSize::SmallInput),
);
}
}
);
Loading
Loading