From 83f25e0552b5052d262ea8d67ce86fe04bd68c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 7 Nov 2014 16:15:56 +0100 Subject: [PATCH] Add more benchmarks. --- benches/common/macros.rs | 114 ++++++++++++++++++++++++ benches/construction.rs | 18 ++++ benches/dmat.rs | 89 +++++++++++++++++++ benches/mat.rs | 181 ++++++--------------------------------- benches/quat.rs | 22 +++++ benches/vec.rs | 80 +++++++++-------- 6 files changed, 315 insertions(+), 189 deletions(-) create mode 100644 benches/common/macros.rs create mode 100644 benches/construction.rs create mode 100644 benches/dmat.rs create mode 100644 benches/quat.rs diff --git a/benches/common/macros.rs b/benches/common/macros.rs new file mode 100644 index 00000000..0dab3abb --- /dev/null +++ b/benches/common/macros.rs @@ -0,0 +1,114 @@ +#![macro_escape] + +macro_rules! bench_binop( + ($name: ident, $t1: ty, $t2: ty, $binop: ident) => { + #[bench] + fn $name(bh: &mut Bencher) { + const LEN: uint = 1 << 13; + + let mut rng = IsaacRng::new_unseeded(); + + let elems1 = Vec::from_fn(LEN, |_| rng.gen::<$t1>()); + let elems2 = Vec::from_fn(LEN, |_| rng.gen::<$t2>()); + let mut i = 0; + + bh.iter(|| { + i = (i + 1) & (LEN - 1); + + unsafe { + test::black_box(elems1.unsafe_get(i).$binop(elems2.unsafe_get(i))) + } + }) + } + } +) + +macro_rules! bench_binop_na( + ($name: ident, $t1: ty, $t2: ty, $binop: ident) => { + #[bench] + fn $name(bh: &mut Bencher) { + const LEN: uint = 1 << 13; + + let mut rng = IsaacRng::new_unseeded(); + + let elems1 = Vec::from_fn(LEN, |_| rng.gen::<$t1>()); + let elems2 = Vec::from_fn(LEN, |_| rng.gen::<$t2>()); + let mut i = 0; + + bh.iter(|| { + i = (i + 1) & (LEN - 1); + + unsafe { + test::black_box(na::$binop(elems1.unsafe_get(i), elems2.unsafe_get(i))) + } + }) + } + } +) + +macro_rules! bench_unop( + ($name: ident, $t: ty, $unop: ident) => { + #[bench] + fn $name(bh: &mut Bencher) { + const LEN: uint = 1 << 13; + + let mut rng = IsaacRng::new_unseeded(); + + let elems = Vec::from_fn(LEN, |_| rng.gen::<$t>()); + let mut i = 0; + + bh.iter(|| { + i = (i + 1) & (LEN - 1); + + unsafe { + test::black_box(na::$unop(elems.unsafe_get(i))) + } + }) + } + } +) + +macro_rules! bench_unop_self( + ($name: ident, $t: ty, $unop: ident) => { + #[bench] + fn $name(bh: &mut Bencher) { + const LEN: uint = 1 << 13; + + let mut rng = IsaacRng::new_unseeded(); + + let mut elems = Vec::from_fn(LEN, |_| rng.gen::<$t>()); + let mut i = 0; + + bh.iter(|| { + i = (i + 1) & (LEN - 1); + + unsafe { + test::black_box(elems.unsafe_mut(i).$unop()) + } + }) + } + } +) + +macro_rules! bench_construction( + ($name: ident, $constructor: path $(, $args: ident: $types: ty)*) => { + #[bench] + fn $name(bh: &mut Bencher) { + const LEN: uint = 1 << 13; + + let mut rng = IsaacRng::new_unseeded(); + + $(let $args = Vec::from_fn(LEN, |_| rng.gen::<$types>());)* + let mut i = 0; + + bh.iter(|| { + i = (i + 1) & (LEN - 1); + + unsafe { + let res = $constructor($(*$args.unsafe_get(i),)*); + test::black_box(res) + } + }) + } + } +) diff --git a/benches/construction.rs b/benches/construction.rs new file mode 100644 index 00000000..02571de7 --- /dev/null +++ b/benches/construction.rs @@ -0,0 +1,18 @@ +#![feature(macro_rules)] + +extern crate test; +extern crate "nalgebra" as na; + +use std::rand::{IsaacRng, Rng}; +use test::Bencher; +use na::{UnitQuat, Rot2, Rot3, Vec1, Vec3}; + +#[path="common/macros.rs"] +mod macros; + +bench_construction!(_bench_quat_from_axisangle, UnitQuat::new, axisangle: Vec3) +bench_construction!(_bench_rot2_from_axisangle, Rot2::new, axisangle: Vec1) +bench_construction!(_bench_rot3_from_axisangle, Rot3::new, axisangle: Vec3) + +bench_construction!(_bench_quat_from_euler_angles, UnitQuat::new_with_euler_angles, roll: f32, pitch: f32, yaw: f32) +bench_construction!(_bench_rot3_from_euler_angles, Rot3::new_with_euler_angles, roll: f32, pitch: f32, yaw: f32) diff --git a/benches/dmat.rs b/benches/dmat.rs new file mode 100644 index 00000000..e6e3b82e --- /dev/null +++ b/benches/dmat.rs @@ -0,0 +1,89 @@ +#![feature(macro_rules)] + +extern crate test; +extern crate "nalgebra" as na; + +use std::rand::{IsaacRng, Rng}; +use test::Bencher; +use na::{Inv}; +use na::{Vec2, Vec3, Vec4, Vec5, Vec6, DVec, Mat2, Mat3, Mat4, Mat5, Mat6, DMat}; + +macro_rules! bench_mul_dmat( + ($bh: expr, $nrows: expr, $ncols: expr) => { + { + let a: DMat = DMat::new_random($nrows, $ncols); + let mut b: DMat = DMat::new_random($nrows, $ncols); + + $bh.iter(|| { + for _ in range(0u, 1000) { + b = a * b; + } + }) + } + } +) + +#[bench] +fn bench_mul_dmat2(bh: &mut Bencher) { + bench_mul_dmat!(bh, 2, 2) +} + +#[bench] +fn bench_mul_dmat3(bh: &mut Bencher) { + bench_mul_dmat!(bh, 3, 3) +} + +#[bench] +fn bench_mul_dmat4(bh: &mut Bencher) { + bench_mul_dmat!(bh, 4, 4) +} + +#[bench] +fn bench_mul_dmat5(bh: &mut Bencher) { + bench_mul_dmat!(bh, 5, 5) +} + +#[bench] +fn bench_mul_dmat6(bh: &mut Bencher) { + bench_mul_dmat!(bh, 6, 6) +} + +macro_rules! bench_mul_dmat_dvec( + ($bh: expr, $nrows: expr, $ncols: expr) => { + { + let m : DMat = DMat::new_random($nrows, $ncols); + let mut v : DVec = DVec::new_random($ncols); + + $bh.iter(|| { + for _ in range(0u, 1000) { + v = m * v + } + }) + } + } +) + +#[bench] +fn bench_mul_dmat_dvec2(bh: &mut Bencher) { + bench_mul_dmat_dvec!(bh, 2, 2) +} + +#[bench] +fn bench_mul_dmat_dvec3(bh: &mut Bencher) { + bench_mul_dmat_dvec!(bh, 3, 3) +} + +#[bench] +fn bench_mul_dmat_dvec4(bh: &mut Bencher) { + bench_mul_dmat_dvec!(bh, 4, 4) +} + +#[bench] +fn bench_mul_dmat_dvec5(bh: &mut Bencher) { + bench_mul_dmat_dvec!(bh, 5, 5) +} + +#[bench] +fn bench_mul_dmat_dvec6(bh: &mut Bencher) { + bench_mul_dmat_dvec!(bh, 6, 6) +} diff --git a/benches/mat.rs b/benches/mat.rs index 2f55246a..1ef956a9 100644 --- a/benches/mat.rs +++ b/benches/mat.rs @@ -3,166 +3,41 @@ extern crate test; extern crate "nalgebra" as na; -use std::rand::random; +use std::rand::{IsaacRng, Rng}; use test::Bencher; -use na::{Vec2, Vec3, Vec4, Vec5, Vec6, DVec, Mat2, Mat3, Mat4, Mat5, Mat6, DMat}; +use na::{Vec2, Vec3, Vec4, Mat2, Mat3, Mat4}; -macro_rules! bench_mul_mat( - ($bh: expr, $t: ty) => { - { - let a: $t = random(); - let mut b: $t = random(); +#[path="common/macros.rs"] +mod macros; - $bh.iter(|| { - for _ in range(0u, 1000) { - b = a * b; - } - }) - } - } -) +bench_binop!(_bench_mat2_mul_m, Mat2, Mat2, mul) +bench_binop!(_bench_mat3_mul_m, Mat3, Mat3, mul) +bench_binop!(_bench_mat4_mul_m, Mat4, Mat4, mul) -#[bench] -fn bench_mul_mat2(bh: &mut Bencher) { - bench_mul_mat!(bh, Mat2) -} +bench_binop!(_bench_mat2_add_m, Mat2, Mat2, add) +bench_binop!(_bench_mat3_add_m, Mat3, Mat3, add) +bench_binop!(_bench_mat4_add_m, Mat4, Mat4, add) -#[bench] -fn bench_mul_mat3(bh: &mut Bencher) { - bench_mul_mat!(bh, Mat3) -} +bench_binop!(_bench_mat2_sub_m, Mat2, Mat2, sub) +bench_binop!(_bench_mat3_sub_m, Mat3, Mat3, sub) +bench_binop!(_bench_mat4_sub_m, Mat4, Mat4, sub) -#[bench] -fn bench_mul_mat4(bh: &mut Bencher) { - bench_mul_mat!(bh, Mat4) -} +bench_binop!(_bench_mat2_mul_v, Mat2, Vec2, mul) +bench_binop!(_bench_mat3_mul_v, Mat3, Vec3, mul) +bench_binop!(_bench_mat4_mul_v, Mat4, Vec4, mul) -#[bench] -fn bench_mul_mat5(bh: &mut Bencher) { - bench_mul_mat!(bh, Mat5) -} +bench_binop!(_bench_mat2_mul_s, Mat2, f32, mul) +bench_binop!(_bench_mat3_mul_s, Mat3, f32, mul) +bench_binop!(_bench_mat4_mul_s, Mat4, f32, mul) -#[bench] -fn bench_mul_mat6(bh: &mut Bencher) { - bench_mul_mat!(bh, Mat6) -} +bench_binop!(_bench_mat2_div_s, Mat2, f32, div) +bench_binop!(_bench_mat3_div_s, Mat3, f32, div) +bench_binop!(_bench_mat4_div_s, Mat4, f32, div) -macro_rules! bench_mul_dmat( - ($bh: expr, $nrows: expr, $ncols: expr) => { - { - let a: DMat = DMat::new_random($nrows, $ncols); - let mut b: DMat = DMat::new_random($nrows, $ncols); +bench_unop!(_bench_mat2_inv, Mat2, inv) +bench_unop!(_bench_mat3_inv, Mat3, inv) +bench_unop!(_bench_mat4_inv, Mat4, inv) - $bh.iter(|| { - for _ in range(0u, 1000) { - b = a * b; - } - }) - } - } -) - -#[bench] -fn bench_mul_dmat2(bh: &mut Bencher) { - bench_mul_dmat!(bh, 2, 2) -} - -#[bench] -fn bench_mul_dmat3(bh: &mut Bencher) { - bench_mul_dmat!(bh, 3, 3) -} - -#[bench] -fn bench_mul_dmat4(bh: &mut Bencher) { - bench_mul_dmat!(bh, 4, 4) -} - -#[bench] -fn bench_mul_dmat5(bh: &mut Bencher) { - bench_mul_dmat!(bh, 5, 5) -} - -#[bench] -fn bench_mul_dmat6(bh: &mut Bencher) { - bench_mul_dmat!(bh, 6, 6) -} - -macro_rules! bench_mul_mat_vec( - ($bh: expr, $tm: ty, $tv: ty) => { - { - let m : $tm = random(); - let mut v : $tv = random(); - - $bh.iter(|| { - for _ in range(0u, 1000) { - v = m * v - } - }) - } - } -) - -#[bench] -fn bench_mul_mat_vec2(bh: &mut Bencher) { - bench_mul_mat_vec!(bh, Mat2, Vec2) -} - -#[bench] -fn bench_mul_mat_vec3(bh: &mut Bencher) { - bench_mul_mat_vec!(bh, Mat3, Vec3) -} - -#[bench] -fn bench_mul_mat_vec4(bh: &mut Bencher) { - bench_mul_mat_vec!(bh, Mat4, Vec4) -} - -#[bench] -fn bench_mul_mat_vec5(bh: &mut Bencher) { - bench_mul_mat_vec!(bh, Mat5, Vec5) -} - -#[bench] -fn bench_mul_mat_vec6(bh: &mut Bencher) { - bench_mul_mat_vec!(bh, Mat6, Vec6) -} - -macro_rules! bench_mul_dmat_dvec( - ($bh: expr, $nrows: expr, $ncols: expr) => { - { - let m : DMat = DMat::new_random($nrows, $ncols); - let mut v : DVec = DVec::new_random($ncols); - - $bh.iter(|| { - for _ in range(0u, 1000) { - v = m * v - } - }) - } - } -) - -#[bench] -fn bench_mul_dmat_dvec2(bh: &mut Bencher) { - bench_mul_dmat_dvec!(bh, 2, 2) -} - -#[bench] -fn bench_mul_dmat_dvec3(bh: &mut Bencher) { - bench_mul_dmat_dvec!(bh, 3, 3) -} - -#[bench] -fn bench_mul_dmat_dvec4(bh: &mut Bencher) { - bench_mul_dmat_dvec!(bh, 4, 4) -} - -#[bench] -fn bench_mul_dmat_dvec5(bh: &mut Bencher) { - bench_mul_dmat_dvec!(bh, 5, 5) -} - -#[bench] -fn bench_mul_dmat_dvec6(bh: &mut Bencher) { - bench_mul_dmat_dvec!(bh, 6, 6) -} +bench_unop!(_bench_mat2_transpose, Mat2, transpose) +bench_unop!(_bench_mat3_transpose, Mat3, transpose) +bench_unop!(_bench_mat4_transpose, Mat4, transpose) diff --git a/benches/quat.rs b/benches/quat.rs new file mode 100644 index 00000000..63224be3 --- /dev/null +++ b/benches/quat.rs @@ -0,0 +1,22 @@ +#![feature(macro_rules)] + +extern crate test; +extern crate "nalgebra" as na; + +use std::rand::{IsaacRng, Rng}; +use test::Bencher; +use na::{Quat, UnitQuat, Vec3}; + +#[path="common/macros.rs"] +mod macros; + +bench_binop!(_bench_quat_add_q, Quat, Quat, add) +bench_binop!(_bench_quat_sub_q, Quat, Quat, sub) +bench_binop!(_bench_quat_mul_q, Quat, Quat, mul) +// bench_binop!(_bench_quat_div_q, Quat, Quat, div) +bench_binop!(_bench_quat_mul_v, UnitQuat, Vec3, mul) +bench_binop!(_bench_quat_mul_s, Quat, f32, mul) +bench_binop!(_bench_quat_div_s, Quat, f32, div) +bench_unop!(_bench_quat_inv, Quat, inv) +bench_unop_self!(_bench_quat_conjugate, Quat, conjugate) +bench_unop!(_bench_quat_normalize, Quat, normalize) diff --git a/benches/vec.rs b/benches/vec.rs index 871d7f0d..d0a00fa5 100644 --- a/benches/vec.rs +++ b/benches/vec.rs @@ -3,47 +3,55 @@ extern crate test; extern crate "nalgebra" as na; -use std::rand::random; +use std::rand::{IsaacRng, Rng}; use test::Bencher; -use na::{Vec2, Vec3, Vec4, Vec5, Vec6}; +use na::{Vec2, Vec3, Vec4}; -macro_rules! bench_dot_vec( - ($bh: expr, $t: ty) => { - { - let a: $t = random(); - let b: $t = random(); - let mut d = 0.0; +#[path="common/macros.rs"] +mod macros; - $bh.iter(|| { - for _ in range(0u, 1000) { - d = d + na::dot(&a, &b); - } - }) - } - } -) +bench_binop!(_bench_vec2_add_v, Vec2, Vec2, add) +bench_binop!(_bench_vec3_add_v, Vec3, Vec3, add) +bench_binop!(_bench_vec4_add_v, Vec4, Vec4, add) -#[bench] -fn bench_dot_vec2(bh: &mut Bencher) { - bench_dot_vec!(bh, Vec2) -} +bench_binop!(_bench_vec2_sub_v, Vec2, Vec2, sub) +bench_binop!(_bench_vec3_sub_v, Vec3, Vec3, sub) +bench_binop!(_bench_vec4_sub_v, Vec4, Vec4, sub) -#[bench] -fn bench_dot_vec3(bh: &mut Bencher) { - bench_dot_vec!(bh, Vec3) -} +bench_binop!(_bench_vec2_mul_v, Vec2, Vec2, mul) +bench_binop!(_bench_vec3_mul_v, Vec3, Vec3, mul) +bench_binop!(_bench_vec4_mul_v, Vec4, Vec4, mul) -#[bench] -fn bench_dot_vec4(bh: &mut Bencher) { - bench_dot_vec!(bh, Vec4) -} +bench_binop!(_bench_vec2_div_v, Vec2, Vec2, div) +bench_binop!(_bench_vec3_div_v, Vec3, Vec3, div) +bench_binop!(_bench_vec4_div_v, Vec4, Vec4, div) -#[bench] -fn bench_dot_vec5(bh: &mut Bencher) { - bench_dot_vec!(bh, Vec5) -} +bench_binop!(_bench_vec2_add_s, Vec2, f32, add) +bench_binop!(_bench_vec3_add_s, Vec3, f32, add) +bench_binop!(_bench_vec4_add_s, Vec4, f32, add) -#[bench] -fn bench_dot_vec6(bh: &mut Bencher) { - bench_dot_vec!(bh, Vec6) -} +bench_binop!(_bench_vec2_sub_s, Vec2, f32, sub) +bench_binop!(_bench_vec3_sub_s, Vec3, f32, sub) +bench_binop!(_bench_vec4_sub_s, Vec4, f32, sub) + +bench_binop!(_bench_vec2_mul_s, Vec2, f32, mul) +bench_binop!(_bench_vec3_mul_s, Vec3, f32, mul) +bench_binop!(_bench_vec4_mul_s, Vec4, f32, mul) + +bench_binop!(_bench_vec2_div_s, Vec2, f32, div) +bench_binop!(_bench_vec3_div_s, Vec3, f32, div) +bench_binop!(_bench_vec4_div_s, Vec4, f32, div) + +bench_binop_na!(_bench_vec2_dot, Vec2, Vec2, dot) +bench_binop_na!(_bench_vec3_dot, Vec3, Vec3, dot) +bench_binop_na!(_bench_vec4_dot, Vec4, Vec4, dot) + +bench_binop_na!(_bench_vec3_cross, Vec3, Vec3, cross) + +bench_unop!(_bench_vec2_norm, Vec2, norm) +bench_unop!(_bench_vec3_norm, Vec3, norm) +bench_unop!(_bench_vec4_norm, Vec4, norm) + +bench_unop!(_bench_vec2_normalize, Vec2, normalize) +bench_unop!(_bench_vec3_normalize, Vec3, normalize) +bench_unop!(_bench_vec4_normalize, Vec4, normalize)