203 lines
5.6 KiB
Rust
203 lines
5.6 KiB
Rust
use bencher::{benchmark_group, benchmark_main, black_box, Bencher};
|
|
use rand::{rngs::SmallRng, FromEntropy, Rng};
|
|
use rmath::{CrossProduct, DotProduct, Normal, Point, Vector, Xform, XformFull};
|
|
|
|
//----
|
|
|
|
fn vector_cross_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let v1 = Vector::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
let v2 = Vector::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
for _ in 0..10000 {
|
|
black_box(black_box(v1).cross(black_box(v2)));
|
|
}
|
|
});
|
|
}
|
|
|
|
fn vector_dot_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let v1 = Vector::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
let v2 = Vector::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
for _ in 0..10000 {
|
|
black_box(black_box(v1).dot(black_box(v2)));
|
|
}
|
|
});
|
|
}
|
|
|
|
fn xform_vector_mul_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let v = Vector::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
let x = Xform::new(
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
);
|
|
for _ in 0..10000 {
|
|
black_box(black_box(v).xform(black_box(&x)));
|
|
}
|
|
});
|
|
}
|
|
|
|
fn xform_point_mul_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let p = Point::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
let x = Xform::new(
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
);
|
|
for _ in 0..10000 {
|
|
black_box(black_box(p).xform(black_box(&x)));
|
|
}
|
|
});
|
|
}
|
|
|
|
fn xform_point_mul_inv_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let p = Point::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
let x = Xform::new(
|
|
1.0,
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
1.0,
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
1.0,
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
)
|
|
.to_full()
|
|
.unwrap();
|
|
for _ in 0..10000 {
|
|
black_box(black_box(p).xform_inv(black_box(&x)));
|
|
}
|
|
});
|
|
}
|
|
|
|
fn xform_normal_mul_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let n = Normal::new(rng.gen::<f32>(), rng.gen::<f32>(), rng.gen::<f32>());
|
|
let x = Xform::new(
|
|
1.0,
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
1.0,
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
1.0,
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
)
|
|
.to_full()
|
|
.unwrap();
|
|
for _ in 0..10000 {
|
|
black_box(black_box(n).xform(black_box(&x)));
|
|
}
|
|
});
|
|
}
|
|
|
|
fn xform_xform_mul_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let x1 = Xform::new(
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
);
|
|
let x2 = Xform::new(
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
);
|
|
for _ in 0..10000 {
|
|
black_box(black_box(x1).compose(black_box(&x2)));
|
|
}
|
|
});
|
|
}
|
|
|
|
fn xform_to_xformfull_10000(bench: &mut Bencher) {
|
|
let mut rng = SmallRng::from_entropy();
|
|
bench.iter(|| {
|
|
let x = Xform::new(
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
rng.gen::<f32>(),
|
|
);
|
|
for _ in 0..10000 {
|
|
black_box(black_box(x).to_full());
|
|
}
|
|
});
|
|
}
|
|
|
|
//----
|
|
|
|
benchmark_group!(
|
|
benches,
|
|
vector_cross_10000,
|
|
vector_dot_10000,
|
|
xform_vector_mul_10000,
|
|
xform_point_mul_10000,
|
|
xform_point_mul_inv_10000,
|
|
xform_normal_mul_10000,
|
|
xform_xform_mul_10000,
|
|
xform_to_xformfull_10000,
|
|
);
|
|
benchmark_main!(benches);
|