From 549aea642704335c9b1cf9e464130fa8a1b7d330 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Thu, 23 Oct 2014 11:15:27 +0200 Subject: [PATCH 1/3] Use Cargo to run the benchmarks Naming the benchmark folder benches instead of bench lets Cargo run the benchmarks one by one like the test suite. The directory structure was modified accordingly also getting rid of the now superfluous files. --- bench/Makefile | 11 ---------- bench/bench.rs | 39 ------------------------------------ {bench => benches}/matrix.rs | 5 +++++ 3 files changed, 5 insertions(+), 50 deletions(-) delete mode 100644 bench/Makefile delete mode 100644 bench/bench.rs rename {bench => benches}/matrix.rs (99%) diff --git a/bench/Makefile b/bench/Makefile deleted file mode 100644 index 5ea2303..0000000 --- a/bench/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -all: bench - -bench: *.rs ../src/*.rs - (cd .. && cargo build) - rustc -L ../target --test bench.rs - -run: bench - ./bench --bench - -clean: - rm bench diff --git a/bench/bench.rs b/bench/bench.rs deleted file mode 100644 index 2603bf2..0000000 --- a/bench/bench.rs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#![feature(globs)] - -extern crate cgmath; -extern crate test; - -// pub mod array; -pub mod matrix; -// pub mod quaternion; -//pub mod vector; - -//pub mod angle; -//pub mod plane; -//pub mod point; -// pub mod ray; -// pub mod rotation; -//pub mod transform; - -// pub mod projection; - -// pub mod aabb; -// pub mod cylinder; -// pub mod frustum; -// pub mod intersect; -// pub mod obb; -//pub mod sphere; diff --git a/bench/matrix.rs b/benches/matrix.rs similarity index 99% rename from bench/matrix.rs rename to benches/matrix.rs index f167a12..f1f7589 100644 --- a/bench/matrix.rs +++ b/benches/matrix.rs @@ -13,6 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +#![feature(globs)] + +extern crate cgmath; +extern crate test; + use cgmath::*; use test::Bencher; From 3e73403ca260607549ca8d66423ca3e2b959f8b3 Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Thu, 23 Oct 2014 11:30:59 +0200 Subject: [PATCH 2/3] Adapt Travis CI integration to benchmark change --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c781f8a..98df6c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ env: script: - cargo build - cargo test - - make -C bench + - cargo bench - cargo doc after_script: # the doc directory needs to be in the root for rust-ci From 0bbefed4bcbc36569a34764e5399bcac07323325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 7 Nov 2014 16:13:54 +0100 Subject: [PATCH 3/3] Improve the benchmark suite. As a side effect, this adds `Rand` deriving to the benchmarked strucures. --- benches/common/macros.rs | 107 ++++++++++++++ benches/construction.rs | 60 ++++++++ benches/mat.rs | 59 ++++++++ benches/matrix.rs | 309 --------------------------------------- benches/quat.rs | 37 +++++ benches/vec.rs | 74 ++++++++++ src/angle.rs | 4 +- src/matrix.rs | 6 +- src/quaternion.rs | 2 +- src/vector.rs | 2 +- 10 files changed, 344 insertions(+), 316 deletions(-) create mode 100644 benches/common/macros.rs create mode 100644 benches/construction.rs create mode 100644 benches/mat.rs delete mode 100644 benches/matrix.rs create mode 100644 benches/quat.rs create mode 100644 benches/vec.rs diff --git a/benches/common/macros.rs b/benches/common/macros.rs new file mode 100644 index 0000000..1f154c8 --- /dev/null +++ b/benches/common/macros.rs @@ -0,0 +1,107 @@ +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directionectory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![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_deref( + ($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_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 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, $t: ty, $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: $t = $constructor($(*$args.unsafe_get(i),)*); + test::black_box(res) + } + }) + } + } +) diff --git a/benches/construction.rs b/benches/construction.rs new file mode 100644 index 0000000..6e008c3 --- /dev/null +++ b/benches/construction.rs @@ -0,0 +1,60 @@ +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directionectory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![feature(macro_rules)] + +extern crate test; +extern crate cgmath; + +use std::rand::{IsaacRng, Rng}; +use test::Bencher; +use cgmath::{Quaternion, Basis2, Basis3, Vector3, Rotation2, Rotation3, Rad}; + +#[path="common/macros.rs"] +mod macros; + +fn bench_from_axis_angle>(bh: &mut Bencher) { + const LEN: uint = 1 << 13; + + let mut rng = IsaacRng::new_unseeded(); + + let axis = Vec::from_fn(LEN, |_| rng.gen::>()); + let angle = Vec::from_fn(LEN, |_| rng.gen::>()); + let mut i = 0; + + bh.iter(|| { + i = (i + 1) & (LEN - 1); + + unsafe { + let res: T = Rotation3::from_axis_angle(axis.unsafe_get(i), *angle.unsafe_get(i)); + test::black_box(res) + } + }) +} + +#[bench] +fn _bench_quat_from_axisangle(bh: &mut Bencher) { + bench_from_axis_angle::>(bh) +} + +#[bench] +fn _bench_rot3_from_axisangle(bh: &mut Bencher) { + bench_from_axis_angle::>(bh) +} + +bench_construction!(_bench_rot2_from_axisangle, Basis2, Rotation2::from_angle, angle: Rad) + +bench_construction!(_bench_quat_from_euler_angles, Quaternion, Rotation3::from_euler, roll: Rad, pitch: Rad, yaw: Rad) +bench_construction!(_bench_rot3_from_euler_angles, Basis3, Rotation3::from_euler, roll: Rad, pitch: Rad, yaw: Rad) diff --git a/benches/mat.rs b/benches/mat.rs new file mode 100644 index 0000000..5ee510d --- /dev/null +++ b/benches/mat.rs @@ -0,0 +1,59 @@ +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directionectory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![feature(macro_rules)] +#![feature(globs)] + +extern crate test; +extern crate cgmath; + +use std::rand::{IsaacRng, Rng}; +use test::Bencher; +use cgmath::*; + +#[path="common/macros.rs"] +mod macros; + +bench_binop!(_bench_matrix2_mul_m, Matrix2, Matrix2, mul_m) +bench_binop!(_bench_matrix3_mul_m, Matrix3, Matrix3, mul_m) +bench_binop!(_bench_matrix4_mul_m, Matrix4, Matrix4, mul_m) + +bench_binop!(_bench_matrix2_add_m, Matrix2, Matrix2, add_m) +bench_binop!(_bench_matrix3_add_m, Matrix3, Matrix3, add_m) +bench_binop!(_bench_matrix4_add_m, Matrix4, Matrix4, add_m) + +bench_binop!(_bench_matrix2_sub_m, Matrix2, Matrix2, sub_m) +bench_binop!(_bench_matrix3_sub_m, Matrix3, Matrix3, sub_m) +bench_binop!(_bench_matrix4_sub_m, Matrix4, Matrix4, sub_m) + +bench_binop!(_bench_matrix2_mul_v, Matrix2, Vector2, mul_v) +bench_binop!(_bench_matrix3_mul_v, Matrix3, Vector3, mul_v) +bench_binop!(_bench_matrix4_mul_v, Matrix4, Vector4, mul_v) + +bench_binop_deref!(_bench_matrix2_mul_s, Matrix2, f32, mul_s) +bench_binop_deref!(_bench_matrix3_mul_s, Matrix3, f32, mul_s) +bench_binop_deref!(_bench_matrix4_mul_s, Matrix4, f32, mul_s) + +bench_binop_deref!(_bench_matrix2_div_s, Matrix2, f32, div_s) +bench_binop_deref!(_bench_matrix3_div_s, Matrix3, f32, div_s) +bench_binop_deref!(_bench_matrix4_div_s, Matrix4, f32, div_s) + +bench_unop!(_bench_matrix2_invert, Matrix2, invert) +bench_unop!(_bench_matrix3_invert, Matrix3, invert) +bench_unop!(_bench_matrix4_invert, Matrix4, invert) + +bench_unop!(_bench_matrix2_transpose, Matrix2, transpose) +bench_unop!(_bench_matrix3_transpose, Matrix3, transpose) +bench_unop!(_bench_matrix4_transpose, Matrix4, transpose) diff --git a/benches/matrix.rs b/benches/matrix.rs deleted file mode 100644 index f1f7589..0000000 --- a/benches/matrix.rs +++ /dev/null @@ -1,309 +0,0 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#![feature(globs)] - -extern crate cgmath; -extern crate test; - -use cgmath::*; - -use test::Bencher; - -pub mod matrix2 { - use cgmath::*; - - pub static A: Matrix2 = Matrix2 { x: Vector2 { x: 1.0, y: 3.0 }, - y: Vector2 { x: 2.0, y: 4.0 } }; - pub static B: Matrix2 = Matrix2 { x: Vector2 { x: 2.0, y: 4.0 }, - y: Vector2 { x: 3.0, y: 5.0 } }; -} - -pub mod matrix3 { - use cgmath::*; - - pub static A: Matrix3 = Matrix3 { x: Vector3 { x: 1.0, y: 4.0, z: 7.0 }, - y: Vector3 { x: 2.0, y: 5.0, z: 8.0 }, - z: Vector3 { x: 3.0, y: 6.0, z: 9.0 } }; - pub static B: Matrix3 = Matrix3 { x: Vector3 { x: 2.0, y: 5.0, z: 8.0 }, - y: Vector3 { x: 3.0, y: 6.0, z: 9.0 }, - z: Vector3 { x: 4.0, y: 7.0, z: 10.0 } }; -} - -pub mod matrix4 { - use cgmath::*; - - pub static A: Matrix4 = Matrix4 { x: Vector4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 }, - y: Vector4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, - z: Vector4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, - w: Vector4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } }; - pub static B: Matrix4 = Matrix4 { x: Vector4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, - y: Vector4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, - z: Vector4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 }, - w: Vector4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } }; -} - -#[bench] -fn bench_matrix2_mul_m(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a = matrix_a.mul_m(&matrix2::B); }) -} - -#[bench] -fn bench_matrix3_mul_m(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a = matrix_a.mul_m(&matrix3::B); }) -} - -#[bench] -fn bench_matrix4_mul_m(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a = matrix_a.mul_m(&matrix4::B); }) -} - -#[bench] -fn bench_matrix2_add_m(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a = matrix_a.add_m(&matrix2::B); }) -} - -#[bench] -fn bench_matrix3_add_m(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a = matrix_a.add_m(&matrix3::B); }) -} - -#[bench] -fn bench_matrix4_add_m(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a = matrix_a.add_m(&matrix4::B); }) -} - -#[bench] -fn bench_matrix2_sub_m(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a = matrix_a.sub_m(&matrix2::B); }) -} - -#[bench] -fn bench_matrix3_sub_m(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a = matrix_a.sub_m(&matrix3::B); }) -} - -#[bench] -fn bench_matrix4_sub_m(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a = matrix_a.sub_m(&matrix4::B); }) -} - -#[bench] -fn bench_matrix2_mul_s(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a = matrix_a.mul_s(2.0); }) -} - -#[bench] -fn bench_matrix3_mul_s(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a = matrix_a.mul_s(2.0); }) -} - -#[bench] -fn bench_matrix4_mul_s(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a = matrix_a.mul_s(2.0); }) -} - -#[bench] -fn bench_matrix2_div_s(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a = matrix_a.div_s(2.); }) -} - -#[bench] -fn bench_matrix3_div_s(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a = matrix_a.div_s(2.); }) -} - -#[bench] -fn bench_matrix4_div_s(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a = matrix_a.div_s(2.); }) -} - -#[bench] -fn bench_matrix2_rem_s(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a = matrix_a.rem_s(2.); }) -} - -#[bench] -fn bench_matrix3_rem_s(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a = matrix_a.rem_s(2.); }) -} - -#[bench] -fn bench_matrix4_rem_s(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a = matrix_a.rem_s(2.); }) -} - -#[bench] -fn bench_matrix2_neg_self(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a.neg_self(); }) -} - -#[bench] -fn bench_matrix3_neg_self(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a.neg_self(); }) -} - -#[bench] -fn bench_matrix4_neg_self(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a.neg_self(); }) -} - -#[bench] -fn bench_matrix2_div_self_s(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a.div_self_s(2.); }) -} - -#[bench] -fn bench_matrix3_div_self_s(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a.div_self_s(2.); }) -} - -#[bench] -fn bench_matrix4_div_self_s(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a.div_self_s(2.); }) -} - -#[bench] -fn bench_matrix2_rem_self_s(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a.rem_self_s(2.); }) -} - -#[bench] -fn bench_matrix3_rem_self_s(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a.rem_self_s(2.); }) -} - -#[bench] -fn bench_matrix4_rem_self_s(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a.rem_self_s(2.); }) -} - -#[bench] -fn bench_matrix2_mul_self_m(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a.mul_self_m(&matrix2::B); }) -} - -#[bench] -fn bench_matrix3_mul_self_m(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a.mul_self_m(&matrix3::B); }) -} - -#[bench] -fn bench_matrix4_mul_self_m(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a.mul_self_m(&matrix4::B); }) -} - -#[bench] -fn bench_matrix2_add_self_m(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a.add_self_m(&matrix2::B); }) -} - -#[bench] -fn bench_matrix3_add_self_m(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a.add_self_m(&matrix3::B); }) -} - -#[bench] -fn bench_matrix4_add_self_m(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a.add_self_m(&matrix4::B); }) -} - -#[bench] -fn bench_matrix2_sub_self_m(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a.sub_self_m(&matrix2::B); }) -} - -#[bench] -fn bench_matrix3_sub_self_m(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a.sub_self_m(&matrix3::B); }) -} - -#[bench] -fn bench_matrix4_sub_self_m(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a.add_self_m(&matrix4::B); }) -} - - -#[bench] -fn bench_matrix2_transpose(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a = matrix_a.transpose(); }) -} - -#[bench] -fn bench_matrix3_transpose(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a = matrix_a.transpose(); }) -} - -#[bench] -fn bench_matrix4_transpose(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a = matrix_a.transpose(); }) -} - -#[bench] -fn bench_matrix2_transpose_self(b: &mut Bencher) { - let mut matrix_a = matrix2::A.clone(); - b.iter(|| { matrix_a.transpose_self(); }) -} - -#[bench] -fn bench_matrix3_transpose_self(b: &mut Bencher) { - let mut matrix_a = matrix3::A.clone(); - b.iter(|| { matrix_a.transpose_self(); }) -} - -#[bench] -fn bench_matrix4_transpose_self(b: &mut Bencher) { - let mut matrix_a = matrix4::A.clone(); - b.iter(|| { matrix_a.transpose_self(); }) -} diff --git a/benches/quat.rs b/benches/quat.rs new file mode 100644 index 0000000..eee10e0 --- /dev/null +++ b/benches/quat.rs @@ -0,0 +1,37 @@ +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directionectory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![feature(globs)] +#![feature(macro_rules)] + +extern crate test; +extern crate cgmath; + +use std::rand::{IsaacRng, Rng}; +use test::Bencher; +use cgmath::*; + +#[path="common/macros.rs"] +mod macros; + +bench_binop!(_bench_quat_add_q, Quaternion, Quaternion, add_q) +bench_binop!(_bench_quat_sub_q, Quaternion, Quaternion, sub_q) +bench_binop!(_bench_quat_mul_q, Quaternion, Quaternion, mul_q) +bench_binop!(_bench_quat_mul_v, Quaternion, Vector3, mul_v) +bench_binop_deref!(_bench_quat_mul_s, Quaternion, f32, mul_s) +bench_binop_deref!(_bench_quat_div_s, Quaternion, f32, div_s) +bench_unop!(_bench_quat_invert, Quaternion, invert) +bench_unop!(_bench_quat_conjugate, Quaternion, conjugate) +bench_unop!(_bench_quat_normalize, Quaternion, normalize) diff --git a/benches/vec.rs b/benches/vec.rs new file mode 100644 index 0000000..1072f69 --- /dev/null +++ b/benches/vec.rs @@ -0,0 +1,74 @@ +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directionectory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![feature(globs)] +#![feature(macro_rules)] + +extern crate test; +extern crate cgmath; + +use std::rand::{IsaacRng, Rng}; +use test::Bencher; +use cgmath::*; + +#[path="common/macros.rs"] +mod macros; + +bench_binop!(_bench_vector2_add_v, Vector2, Vector2, add_v) +bench_binop!(_bench_vector3_add_v, Vector3, Vector3, add_v) +bench_binop!(_bench_vector4_add_v, Vector4, Vector4, add_v) + +bench_binop!(_bench_vector2_sub_v, Vector2, Vector2, sub_v) +bench_binop!(_bench_vector3_sub_v, Vector3, Vector3, sub_v) +bench_binop!(_bench_vector4_sub_v, Vector4, Vector4, sub_v) + +bench_binop!(_bench_vector2_mul_v, Vector2, Vector2, mul_v) +bench_binop!(_bench_vector3_mul_v, Vector3, Vector3, mul_v) +bench_binop!(_bench_vector4_mul_v, Vector4, Vector4, mul_v) + +bench_binop!(_bench_vector2_div_v, Vector2, Vector2, div_v) +bench_binop!(_bench_vector3_div_v, Vector3, Vector3, div_v) +bench_binop!(_bench_vector4_div_v, Vector4, Vector4, div_v) + +bench_binop_deref!(_bench_vector2_add_s, Vector2, f32, add_s) +bench_binop_deref!(_bench_vector3_add_s, Vector3, f32, add_s) +bench_binop_deref!(_bench_vector4_add_s, Vector4, f32, add_s) + +bench_binop_deref!(_bench_vector2_sub_s, Vector2, f32, sub_s) +bench_binop_deref!(_bench_vector3_sub_s, Vector3, f32, sub_s) +bench_binop_deref!(_bench_vector4_sub_s, Vector4, f32, sub_s) + +bench_binop_deref!(_bench_vector2_mul_s, Vector2, f32, mul_s) +bench_binop_deref!(_bench_vector3_mul_s, Vector3, f32, mul_s) +bench_binop_deref!(_bench_vector4_mul_s, Vector4, f32, mul_s) + +bench_binop_deref!(_bench_vector2_div_s, Vector2, f32, div_s) +bench_binop_deref!(_bench_vector3_div_s, Vector3, f32, div_s) +bench_binop_deref!(_bench_vector4_div_s, Vector4, f32, div_s) + + +bench_binop!(_bench_vector2_dot, Vector2, Vector2, dot) +bench_binop!(_bench_vector3_dot, Vector3, Vector3, dot) +bench_binop!(_bench_vector4_dot, Vector4, Vector4, dot) + +bench_binop!(_bench_vector3_cross, Vector3, Vector3, cross) + +bench_unop!(_bench_vector2_norm, Vector2, length) +bench_unop!(_bench_vector3_norm, Vector3, length) +bench_unop!(_bench_vector4_norm, Vector4, length) + +bench_unop!(_bench_vector2_normalize, Vector2, normalize) +bench_unop!(_bench_vector3_normalize, Vector3, normalize) +bench_unop!(_bench_vector4_normalize, Vector4, normalize) diff --git a/src/angle.rs b/src/angle.rs index 468224e..df34dbc 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -22,10 +22,10 @@ use approx::ApproxEq; use num::BaseFloat; /// An angle, in radians -#[deriving(Clone, PartialEq, PartialOrd, Hash, Encodable, Decodable)] +#[deriving(Clone, PartialEq, PartialOrd, Hash, Encodable, Decodable, Rand)] pub struct Rad { pub s: S } /// An angle, in degrees -#[deriving(Clone, PartialEq, PartialOrd, Hash, Encodable, Decodable)] +#[deriving(Clone, PartialEq, PartialOrd, Hash, Encodable, Decodable, Rand)] pub struct Deg { pub s: S } /// Create a new angle, in radians diff --git a/src/matrix.rs b/src/matrix.rs index 4f5df4f..b1140ee 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -29,15 +29,15 @@ use vector::{Vector, EuclideanVector}; use vector::{Vector2, Vector3, Vector4}; /// A 2 x 2, column major matrix -#[deriving(Clone, PartialEq, Encodable, Decodable)] +#[deriving(Clone, PartialEq, Encodable, Decodable, Rand)] pub struct Matrix2 { pub x: Vector2, pub y: Vector2 } /// A 3 x 3, column major matrix -#[deriving(Clone, PartialEq, Encodable, Decodable)] +#[deriving(Clone, PartialEq, Encodable, Decodable, Rand)] pub struct Matrix3 { pub x: Vector3, pub y: Vector3, pub z: Vector3 } /// A 4 x 4, column major matrix -#[deriving(Clone, PartialEq, Encodable, Decodable)] +#[deriving(Clone, PartialEq, Encodable, Decodable, Rand)] pub struct Matrix4 { pub x: Vector4, pub y: Vector4, pub z: Vector4, pub w: Vector4 } diff --git a/src/quaternion.rs b/src/quaternion.rs index e6d5b26..676210b 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -28,7 +28,7 @@ use vector::{Vector3, Vector, EuclideanVector}; /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// form. -#[deriving(Clone, PartialEq, Encodable, Decodable)] +#[deriving(Clone, PartialEq, Encodable, Decodable, Rand)] pub struct Quaternion { pub s: S, pub v: Vector3 } /// Represents types which can be expressed as a quaternion. diff --git a/src/vector.rs b/src/vector.rs index 27f71e0..9f3280c 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -183,7 +183,7 @@ pub trait Vector: Array1 // Utility macro for generating associated functions for the vectors macro_rules! vec( ($Self:ident <$S:ident> { $($field:ident),+ }, $n:expr) => ( - #[deriving(PartialEq, Eq, Clone, Hash, Encodable, Decodable)] + #[deriving(PartialEq, Eq, Clone, Hash, Encodable, Decodable, Rand)] pub struct $Self { $(pub $field: S),+ } impl<$S> $Self<$S> {