Add more benchmarks.

This commit is contained in:
Sébastien Crozet 2014-11-07 16:15:56 +01:00
parent 226b115f1e
commit 83f25e0552
6 changed files with 315 additions and 189 deletions

114
benches/common/macros.rs Normal file
View File

@ -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)
}
})
}
}
)

18
benches/construction.rs Normal file
View File

@ -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<f32>)
bench_construction!(_bench_rot2_from_axisangle, Rot2::new, axisangle: Vec1<f32>)
bench_construction!(_bench_rot3_from_axisangle, Rot3::new, axisangle: Vec3<f32>)
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)

89
benches/dmat.rs Normal file
View File

@ -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<f64> = DMat::new_random($nrows, $ncols);
let mut b: DMat<f64> = 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<f64> = DMat::new_random($nrows, $ncols);
let mut v : DVec<f64> = 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)
}

View File

@ -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<f32>, Mat2<f32>, mul)
bench_binop!(_bench_mat3_mul_m, Mat3<f32>, Mat3<f32>, mul)
bench_binop!(_bench_mat4_mul_m, Mat4<f32>, Mat4<f32>, mul)
#[bench]
fn bench_mul_mat2(bh: &mut Bencher) {
bench_mul_mat!(bh, Mat2<f64>)
}
bench_binop!(_bench_mat2_add_m, Mat2<f32>, Mat2<f32>, add)
bench_binop!(_bench_mat3_add_m, Mat3<f32>, Mat3<f32>, add)
bench_binop!(_bench_mat4_add_m, Mat4<f32>, Mat4<f32>, add)
#[bench]
fn bench_mul_mat3(bh: &mut Bencher) {
bench_mul_mat!(bh, Mat3<f64>)
}
bench_binop!(_bench_mat2_sub_m, Mat2<f32>, Mat2<f32>, sub)
bench_binop!(_bench_mat3_sub_m, Mat3<f32>, Mat3<f32>, sub)
bench_binop!(_bench_mat4_sub_m, Mat4<f32>, Mat4<f32>, sub)
#[bench]
fn bench_mul_mat4(bh: &mut Bencher) {
bench_mul_mat!(bh, Mat4<f64>)
}
bench_binop!(_bench_mat2_mul_v, Mat2<f32>, Vec2<f32>, mul)
bench_binop!(_bench_mat3_mul_v, Mat3<f32>, Vec3<f32>, mul)
bench_binop!(_bench_mat4_mul_v, Mat4<f32>, Vec4<f32>, mul)
#[bench]
fn bench_mul_mat5(bh: &mut Bencher) {
bench_mul_mat!(bh, Mat5<f64>)
}
bench_binop!(_bench_mat2_mul_s, Mat2<f32>, f32, mul)
bench_binop!(_bench_mat3_mul_s, Mat3<f32>, f32, mul)
bench_binop!(_bench_mat4_mul_s, Mat4<f32>, f32, mul)
#[bench]
fn bench_mul_mat6(bh: &mut Bencher) {
bench_mul_mat!(bh, Mat6<f64>)
}
bench_binop!(_bench_mat2_div_s, Mat2<f32>, f32, div)
bench_binop!(_bench_mat3_div_s, Mat3<f32>, f32, div)
bench_binop!(_bench_mat4_div_s, Mat4<f32>, f32, div)
macro_rules! bench_mul_dmat(
($bh: expr, $nrows: expr, $ncols: expr) => {
{
let a: DMat<f64> = DMat::new_random($nrows, $ncols);
let mut b: DMat<f64> = DMat::new_random($nrows, $ncols);
bench_unop!(_bench_mat2_inv, Mat2<f32>, inv)
bench_unop!(_bench_mat3_inv, Mat3<f32>, inv)
bench_unop!(_bench_mat4_inv, Mat4<f32>, 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<f64>, Vec2<f64>)
}
#[bench]
fn bench_mul_mat_vec3(bh: &mut Bencher) {
bench_mul_mat_vec!(bh, Mat3<f64>, Vec3<f64>)
}
#[bench]
fn bench_mul_mat_vec4(bh: &mut Bencher) {
bench_mul_mat_vec!(bh, Mat4<f64>, Vec4<f64>)
}
#[bench]
fn bench_mul_mat_vec5(bh: &mut Bencher) {
bench_mul_mat_vec!(bh, Mat5<f64>, Vec5<f64>)
}
#[bench]
fn bench_mul_mat_vec6(bh: &mut Bencher) {
bench_mul_mat_vec!(bh, Mat6<f64>, Vec6<f64>)
}
macro_rules! bench_mul_dmat_dvec(
($bh: expr, $nrows: expr, $ncols: expr) => {
{
let m : DMat<f64> = DMat::new_random($nrows, $ncols);
let mut v : DVec<f64> = 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<f32>, transpose)
bench_unop!(_bench_mat3_transpose, Mat3<f32>, transpose)
bench_unop!(_bench_mat4_transpose, Mat4<f32>, transpose)

22
benches/quat.rs Normal file
View File

@ -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<f32>, Quat<f32>, add)
bench_binop!(_bench_quat_sub_q, Quat<f32>, Quat<f32>, sub)
bench_binop!(_bench_quat_mul_q, Quat<f32>, Quat<f32>, mul)
// bench_binop!(_bench_quat_div_q, Quat<f32>, Quat<f32>, div)
bench_binop!(_bench_quat_mul_v, UnitQuat<f32>, Vec3<f32>, mul)
bench_binop!(_bench_quat_mul_s, Quat<f32>, f32, mul)
bench_binop!(_bench_quat_div_s, Quat<f32>, f32, div)
bench_unop!(_bench_quat_inv, Quat<f32>, inv)
bench_unop_self!(_bench_quat_conjugate, Quat<f32>, conjugate)
bench_unop!(_bench_quat_normalize, Quat<f32>, normalize)

View File

@ -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<f32>, Vec2<f32>, add)
bench_binop!(_bench_vec3_add_v, Vec3<f32>, Vec3<f32>, add)
bench_binop!(_bench_vec4_add_v, Vec4<f32>, Vec4<f32>, add)
#[bench]
fn bench_dot_vec2(bh: &mut Bencher) {
bench_dot_vec!(bh, Vec2<f64>)
}
bench_binop!(_bench_vec2_sub_v, Vec2<f32>, Vec2<f32>, sub)
bench_binop!(_bench_vec3_sub_v, Vec3<f32>, Vec3<f32>, sub)
bench_binop!(_bench_vec4_sub_v, Vec4<f32>, Vec4<f32>, sub)
#[bench]
fn bench_dot_vec3(bh: &mut Bencher) {
bench_dot_vec!(bh, Vec3<f64>)
}
bench_binop!(_bench_vec2_mul_v, Vec2<f32>, Vec2<f32>, mul)
bench_binop!(_bench_vec3_mul_v, Vec3<f32>, Vec3<f32>, mul)
bench_binop!(_bench_vec4_mul_v, Vec4<f32>, Vec4<f32>, mul)
#[bench]
fn bench_dot_vec4(bh: &mut Bencher) {
bench_dot_vec!(bh, Vec4<f64>)
}
bench_binop!(_bench_vec2_div_v, Vec2<f32>, Vec2<f32>, div)
bench_binop!(_bench_vec3_div_v, Vec3<f32>, Vec3<f32>, div)
bench_binop!(_bench_vec4_div_v, Vec4<f32>, Vec4<f32>, div)
#[bench]
fn bench_dot_vec5(bh: &mut Bencher) {
bench_dot_vec!(bh, Vec5<f64>)
}
bench_binop!(_bench_vec2_add_s, Vec2<f32>, f32, add)
bench_binop!(_bench_vec3_add_s, Vec3<f32>, f32, add)
bench_binop!(_bench_vec4_add_s, Vec4<f32>, f32, add)
#[bench]
fn bench_dot_vec6(bh: &mut Bencher) {
bench_dot_vec!(bh, Vec6<f64>)
}
bench_binop!(_bench_vec2_sub_s, Vec2<f32>, f32, sub)
bench_binop!(_bench_vec3_sub_s, Vec3<f32>, f32, sub)
bench_binop!(_bench_vec4_sub_s, Vec4<f32>, f32, sub)
bench_binop!(_bench_vec2_mul_s, Vec2<f32>, f32, mul)
bench_binop!(_bench_vec3_mul_s, Vec3<f32>, f32, mul)
bench_binop!(_bench_vec4_mul_s, Vec4<f32>, f32, mul)
bench_binop!(_bench_vec2_div_s, Vec2<f32>, f32, div)
bench_binop!(_bench_vec3_div_s, Vec3<f32>, f32, div)
bench_binop!(_bench_vec4_div_s, Vec4<f32>, f32, div)
bench_binop_na!(_bench_vec2_dot, Vec2<f32>, Vec2<f32>, dot)
bench_binop_na!(_bench_vec3_dot, Vec3<f32>, Vec3<f32>, dot)
bench_binop_na!(_bench_vec4_dot, Vec4<f32>, Vec4<f32>, dot)
bench_binop_na!(_bench_vec3_cross, Vec3<f32>, Vec3<f32>, cross)
bench_unop!(_bench_vec2_norm, Vec2<f32>, norm)
bench_unop!(_bench_vec3_norm, Vec3<f32>, norm)
bench_unop!(_bench_vec4_norm, Vec4<f32>, norm)
bench_unop!(_bench_vec2_normalize, Vec2<f32>, normalize)
bench_unop!(_bench_vec3_normalize, Vec3<f32>, normalize)
bench_unop!(_bench_vec4_normalize, Vec4<f32>, normalize)