-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform.rs
106 lines (92 loc) · 3.51 KB
/
transform.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#![allow(dead_code)]
#![allow(unused_imports)]
#[macro_use]
extern crate criterion;
use core::time::Duration;
use criterion::{black_box, BatchSize, BenchmarkGroup, Criterion, Throughput};
use rand::prelude::*;
use std::fmt;
const LEN: usize = 1_000_000;
const WARM_UP_TIME: Duration = Duration::from_secs(1);
const MEASUREMENT_TIME: Duration = Duration::from_secs(5);
use bevy::prelude::{GlobalTransform, Mat3, Mat4, Quat, Transform, Vec3, Vec4};
fn cmp(c: &mut Criterion) {
let core_ids = core_affinity::get_core_ids().unwrap();
core_affinity::set_for_current(core_ids[0]);
let mut rng = rand::thread_rng();
let trs = Transform {
translation: Vec3::new(rng.gen(), rng.gen(), rng.gen()),
rotation: Quat::from_rotation_ypr(rng.gen(), rng.gen(), rng.gen()),
scale: Vec3::new(rng.gen(), rng.gen(), rng.gen()),
};
let mat = trs.compute_matrix();
let mut group = c.benchmark_group("conversion");
group.bench_with_input("to_mat4", &trs, |b, data| {
b.iter(|| black_box(data.compute_matrix()))
});
group.bench_with_input("from_mat4", &mat, |b, data| {
b.iter(|| black_box(Transform::from_matrix(*data)))
});
group.warm_up_time(WARM_UP_TIME);
group.measurement_time(MEASUREMENT_TIME);
group.finish();
let mut group = c.benchmark_group("transform_point");
group.bench_with_input("trs", &trs, |b, data| {
b.iter(|| black_box(data.mul_vec3(Vec3::ONE)))
});
group.bench_with_input("mat4", &mat, |b, data| {
b.iter(|| black_box(data.transform_point3(Vec3::ONE)))
});
group.warm_up_time(WARM_UP_TIME);
group.measurement_time(MEASUREMENT_TIME);
group.finish();
let mut group = c.benchmark_group("inverse");
group.bench_with_input("trs", &trs, |b, data| {
b.iter(|| {
black_box(
Transform {
translation: -data.translation,
rotation: data.rotation.inverse(),
scale: data.scale.recip(),
}
.compute_matrix(),
)
})
});
group.bench_with_input("mat4", &mat, |b, data| b.iter(|| black_box(data.inverse())));
group.warm_up_time(WARM_UP_TIME);
group.measurement_time(MEASUREMENT_TIME);
group.finish();
let mut group = c.benchmark_group("transform_propagation");
group.bench_with_input("trs", &trs, |b, data| {
b.iter(|| black_box(data.mul_transform(*data)))
});
group.bench_with_input("mat4", &(mat, trs), |b, (mat, trs)| {
b.iter(|| black_box((*mat) * trs.compute_matrix()))
});
group.warm_up_time(WARM_UP_TIME);
group.measurement_time(MEASUREMENT_TIME);
group.finish();
let mut group = c.benchmark_group("right_up_forward");
group.bench_with_input("trs", &trs, |b, data| {
b.iter(|| black_box(data.rotation * Vec3::X))
});
group.bench_with_input("mat4", &mat, |b, data| {
b.iter(|| black_box(Vec3::from(data.x_axis).normalize()))
});
group.warm_up_time(WARM_UP_TIME);
group.measurement_time(MEASUREMENT_TIME);
group.finish();
let mut group = c.benchmark_group("any_direction");
group.bench_with_input("trs", &trs, |b, data| {
b.iter(|| black_box(data.rotation * Vec3::X))
});
group.bench_with_input("mat4", &mat, |b, data| {
b.iter(|| black_box(data.transform_vector3(Vec3::X)))
});
group.warm_up_time(WARM_UP_TIME);
group.measurement_time(MEASUREMENT_TIME);
group.finish();
}
criterion_group!(benches, cmp);
criterion_main!(benches);