From 6d76249d74783a25c6a370863ddf0e4b062f992f Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Sat, 23 Mar 2019 18:01:04 +0100 Subject: [PATCH] Start switching benchmarks to criterion. --- Cargo.toml | 9 +++++++++ benches/common/macros.rs | 42 +++++++++++++++++----------------------- benches/core/matrix.rs | 2 ++ benches/core/mod.rs | 4 ++-- benches/core/vector.rs | 35 ++++++++++++++------------------- benches/lib.rs | 11 ++++++++--- 6 files changed, 54 insertions(+), 49 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 736b9738..fe87f723 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,9 +51,18 @@ pest_derive = { version = "2.0", optional = true } [dev-dependencies] serde_json = "1.0" rand_xorshift = "0.1" +criterion = "0.2" [workspace] members = [ "nalgebra-lapack", "nalgebra-glm" ] +[[bench]] +name = "nalgebra_bench" +harness = false +path = "benches/lib.rs" + [patch.crates-io] alga = { path = "../alga/alga" } + +[profile.bench] +lto = true diff --git a/benches/common/macros.rs b/benches/common/macros.rs index 758336a8..43bcf59d 100644 --- a/benches/common/macros.rs +++ b/benches/common/macros.rs @@ -2,56 +2,52 @@ macro_rules! bench_binop( ($name: ident, $t1: ty, $t2: ty, $binop: ident) => { - #[bench] - fn $name(bh: &mut Bencher) { + fn $name(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let a = rng.gen::<$t1>(); let b = rng.gen::<$t2>(); - bh.iter(|| { + bh.bench_function(stringify!($name), move |bh| bh.iter(|| { a.$binop(b) - }) + })); } } ); macro_rules! bench_binop_ref( ($name: ident, $t1: ty, $t2: ty, $binop: ident) => { - #[bench] - fn $name(bh: &mut Bencher) { + fn $name(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let a = rng.gen::<$t1>(); let b = rng.gen::<$t2>(); - bh.iter(|| { + bh.bench_function(stringify!($name), move |bh| bh.iter(|| { a.$binop(&b) - }) + })); } } ); macro_rules! bench_binop_fn( ($name: ident, $t1: ty, $t2: ty, $binop: path) => { - #[bench] - fn $name(bh: &mut Bencher) { + fn $name(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let a = rng.gen::<$t1>(); let b = rng.gen::<$t2>(); - bh.iter(|| { + bh.bench_function(stringify!($name), move |bh| bh.iter(|| { $binop(&a, &b) - }) + })); } } ); macro_rules! bench_unop_na( ($name: ident, $t: ty, $unop: ident) => { - #[bench] - fn $name(bh: &mut Bencher) { + fn $name(bh: &mut criterion::Criterion) { const LEN: usize = 1 << 13; use rand::SeedableRng; @@ -60,21 +56,20 @@ macro_rules! bench_unop_na( let elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect(); let mut i = 0; - bh.iter(|| { + bh.bench_function(stringify!($name), move |bh| bh.iter(|| { i = (i + 1) & (LEN - 1); unsafe { test::black_box(na::$unop(elems.get_unchecked(i))) } - }) + })); } } ); macro_rules! bench_unop( ($name: ident, $t: ty, $unop: ident) => { - #[bench] - fn $name(bh: &mut Bencher) { + fn $name(bh: &mut criterion::Criterion) { const LEN: usize = 1 << 13; use rand::SeedableRng; @@ -83,21 +78,20 @@ macro_rules! bench_unop( let mut elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect(); let mut i = 0; - bh.iter(|| { + bh.bench_function(stringify!($name), move |bh| bh.iter(|| { i = (i + 1) & (LEN - 1); unsafe { test::black_box(elems.get_unchecked_mut(i).$unop()) } - }) + })); } } ); macro_rules! bench_construction( ($name: ident, $constructor: path, $( $args: ident: $types: ty),*) => { - #[bench] - fn $name(bh: &mut Bencher) { + fn $name(bh: &mut criterion::Criterion) { const LEN: usize = 1 << 13; use rand::SeedableRng; @@ -106,14 +100,14 @@ macro_rules! bench_construction( $(let $args: Vec<$types> = (0usize .. LEN).map(|_| rng.gen::<$types>()).collect();)* let mut i = 0; - bh.iter(|| { + bh.bench_function(stringify!($name), move |bh| bh.iter(|| { i = (i + 1) & (LEN - 1); unsafe { let res = $constructor($(*$args.get_unchecked(i),)*); test::black_box(res) } - }) + })); } } ); diff --git a/benches/core/matrix.rs b/benches/core/matrix.rs index c323cd6e..896f2c97 100644 --- a/benches/core/matrix.rs +++ b/benches/core/matrix.rs @@ -196,3 +196,5 @@ fn mat100_from_fn(bench: &mut Bencher) { fn mat500_from_fn(bench: &mut Bencher) { bench.iter(|| DMatrix::from_fn(500, 500, |a, b| a + b)) } + +criterion_group!(matrix, mat2_transpose); \ No newline at end of file diff --git a/benches/core/mod.rs b/benches/core/mod.rs index 9699a728..f3e633a8 100644 --- a/benches/core/mod.rs +++ b/benches/core/mod.rs @@ -1,2 +1,2 @@ -mod matrix; -mod vector; +pub mod matrix; +pub mod vector; diff --git a/benches/core/vector.rs b/benches/core/vector.rs index 837eb7ca..814f50e7 100644 --- a/benches/core/vector.rs +++ b/benches/core/vector.rs @@ -1,7 +1,6 @@ use na::{DVector, Vector2, Vector3, Vector4, VectorN}; use rand::{IsaacRng, Rng}; use std::ops::{Add, Div, Mul, Sub}; -use test::{self, Bencher}; use typenum::U10000; #[path = "../common/macros.rs"] @@ -48,19 +47,17 @@ bench_unop!(vec4_normalize, Vector4, normalize); bench_binop_ref!(vec10000_dot_f64, VectorN, VectorN, dot); bench_binop_ref!(vec10000_dot_f32, VectorN, VectorN, dot); -#[bench] -fn vec10000_axpy_f64(bh: &mut Bencher) { +fn vec10000_axpy_f64(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let mut a = DVector::new_random(10000); let b = DVector::new_random(10000); let n = rng.gen::(); - bh.iter(|| a.axpy(n, &b, 1.0)) + bh.bench_function("", move |bh| bh.iter(|| a.axpy(n, &b, 1.0))); } -#[bench] -fn vec10000_axpy_beta_f64(bh: &mut Bencher) { +fn vec10000_axpy_beta_f64(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let mut a = DVector::new_random(10000); @@ -68,27 +65,25 @@ fn vec10000_axpy_beta_f64(bh: &mut Bencher) { let n = rng.gen::(); let beta = rng.gen::(); - bh.iter(|| a.axpy(n, &b, beta)) + bh.bench_function("", move |bh| bh.iter(|| a.axpy(n, &b, beta))); } -#[bench] -fn vec10000_axpy_f64_slice(bh: &mut Bencher) { +fn vec10000_axpy_f64_slice(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let mut a = DVector::new_random(10000); let b = DVector::new_random(10000); let n = rng.gen::(); - bh.iter(|| { + bh.bench_function("", move |bh| bh.iter(|| { let mut a = a.fixed_rows_mut::(0); let b = b.fixed_rows::(0); a.axpy(n, &b, 1.0) - }) + })); } -#[bench] -fn vec10000_axpy_f64_static(bh: &mut Bencher) { +fn vec10000_axpy_f64_static(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let mut a = VectorN::::new_random(); @@ -96,22 +91,20 @@ fn vec10000_axpy_f64_static(bh: &mut Bencher) { let n = rng.gen::(); // NOTE: for some reasons, it is much faster if the arument are boxed (Box::new(VectorN...)). - bh.iter(|| a.axpy(n, &b, 1.0)) + bh.bench_function("", move |bh| bh.iter(|| a.axpy(n, &b, 1.0))); } -#[bench] -fn vec10000_axpy_f32(bh: &mut Bencher) { +fn vec10000_axpy_f32(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let mut a = DVector::new_random(10000); let b = DVector::new_random(10000); let n = rng.gen::(); - bh.iter(|| a.axpy(n, &b, 1.0)) + bh.bench_function("", move |bh| bh.iter(|| a.axpy(n, &b, 1.0))); } -#[bench] -fn vec10000_axpy_beta_f32(bh: &mut Bencher) { +fn vec10000_axpy_beta_f32(bh: &mut criterion::Criterion) { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); let mut a = DVector::new_random(10000); @@ -119,5 +112,7 @@ fn vec10000_axpy_beta_f32(bh: &mut Bencher) { let n = rng.gen::(); let beta = rng.gen::(); - bh.iter(|| a.axpy(n, &b, beta)) + bh.bench_function("", move |bh| bh.iter(|| a.axpy(n, &b, beta))); } + +criterion_group!(vector, vec10000_axpy_f64_static); diff --git a/benches/lib.rs b/benches/lib.rs index f788c998..7c8cdc70 100644 --- a/benches/lib.rs +++ b/benches/lib.rs @@ -6,15 +6,20 @@ extern crate rand; extern crate test; extern crate typenum; +#[macro_use] +extern crate criterion; + use na::DMatrix; use rand::{IsaacRng, Rng}; -mod core; -mod geometry; -mod linalg; +pub mod core; +pub mod geometry; +pub mod linalg; fn reproductible_dmatrix(nrows: usize, ncols: usize) -> DMatrix { use rand::SeedableRng; let mut rng = IsaacRng::seed_from_u64(0); DMatrix::::from_fn(nrows, ncols, |_, _| rng.gen()) } + +criterion_main!(core::matrix::matrix, core::vector::vector); \ No newline at end of file