| 1 |
use na::{DMatrix, DVector, Matrix2, Matrix3, Matrix4, OMatrix, U10, Vector2, Vector3, Vector4}; |
| 2 |
use rand::Rng; |
| 3 |
use rand_isaac::IsaacRng; |
| 4 |
use std::ops::{Add, Div, Mul, Sub}; |
| 5 |
|
| 6 |
#[path = "../common/macros.rs"] |
| 7 |
mod macros; |
| 8 |
|
| 9 |
bench_binop!(mat2_mul_m, Matrix2<f32>, Matrix2<f32>, mul); |
| 10 |
bench_binop!(mat3_mul_m, Matrix3<f32>, Matrix3<f32>, mul); |
| 11 |
bench_binop!(mat4_mul_m, Matrix4<f32>, Matrix4<f32>, mul); |
| 12 |
|
| 13 |
bench_binop_ref!(mat2_tr_mul_m, Matrix2<f32>, Matrix2<f32>, tr_mul); |
| 14 |
bench_binop_ref!(mat3_tr_mul_m, Matrix3<f32>, Matrix3<f32>, tr_mul); |
| 15 |
bench_binop_ref!(mat4_tr_mul_m, Matrix4<f32>, Matrix4<f32>, tr_mul); |
| 16 |
|
| 17 |
bench_binop!(mat2_add_m, Matrix2<f32>, Matrix2<f32>, add); |
| 18 |
bench_binop!(mat3_add_m, Matrix3<f32>, Matrix3<f32>, add); |
| 19 |
bench_binop!(mat4_add_m, Matrix4<f32>, Matrix4<f32>, add); |
| 20 |
|
| 21 |
bench_binop!(mat2_sub_m, Matrix2<f32>, Matrix2<f32>, sub); |
| 22 |
bench_binop!(mat3_sub_m, Matrix3<f32>, Matrix3<f32>, sub); |
| 23 |
bench_binop!(mat4_sub_m, Matrix4<f32>, Matrix4<f32>, sub); |
| 24 |
|
| 25 |
bench_binop!(mat2_mul_v, Matrix2<f32>, Vector2<f32>, mul); |
| 26 |
bench_binop!(mat3_mul_v, Matrix3<f32>, Vector3<f32>, mul); |
| 27 |
bench_binop!(mat4_mul_v, Matrix4<f32>, Vector4<f32>, mul); |
| 28 |
|
| 29 |
bench_binop_ref!(mat2_tr_mul_v, Matrix2<f32>, Vector2<f32>, tr_mul); |
| 30 |
bench_binop_ref!(mat3_tr_mul_v, Matrix3<f32>, Vector3<f32>, tr_mul); |
| 31 |
bench_binop_ref!(mat4_tr_mul_v, Matrix4<f32>, Vector4<f32>, tr_mul); |
| 32 |
|
| 33 |
bench_binop!(mat2_mul_s, Matrix2<f32>, f32, mul); |
| 34 |
bench_binop!(mat3_mul_s, Matrix3<f32>, f32, mul); |
| 35 |
bench_binop!(mat4_mul_s, Matrix4<f32>, f32, mul); |
| 36 |
|
| 37 |
bench_binop!(mat2_div_s, Matrix2<f32>, f32, div); |
| 38 |
bench_binop!(mat3_div_s, Matrix3<f32>, f32, div); |
| 39 |
bench_binop!(mat4_div_s, Matrix4<f32>, f32, div); |
| 40 |
|
| 41 |
bench_unop!(mat2_inv, Matrix2<f32>, try_inverse); |
| 42 |
bench_unop!(mat3_inv, Matrix3<f32>, try_inverse); |
| 43 |
bench_unop!(mat4_inv, Matrix4<f32>, try_inverse); |
| 44 |
|
| 45 |
bench_unop!(mat2_transpose, Matrix2<f32>, transpose); |
| 46 |
bench_unop!(mat3_transpose, Matrix3<f32>, transpose); |
| 47 |
bench_unop!(mat4_transpose, Matrix4<f32>, transpose); |
| 48 |
|
| 49 |
fn mat_div_scalar(b: &mut criterion::Criterion) { |
| 50 |
let a = DMatrix::from_row_slice(1000, 1000, &vec![2.0; 1000000]); |
| 51 |
let n = 42.0; |
| 52 |
|
| 53 |
b.bench_function("mat_div_scalar", move |bh| { |
| 54 |
bh.iter(|| { |
| 55 |
let mut aa = a.clone(); |
| 56 |
let mut b = aa.view_mut((0, 0), (1000, 1000)); |
| 57 |
b /= n |
| 58 |
}) |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
fn mat100_add_mat100(bench: &mut criterion::Criterion) { |
| 63 |
let a = DMatrix::<f64>::new_random(100, 100); |
| 64 |
let b = DMatrix::<f64>::new_random(100, 100); |
| 65 |
|
| 66 |
bench.bench_function("mat100_add_mat100", move |bh| bh.iter(|| &a + &b)); |
| 67 |
} |
| 68 |
|
| 69 |
fn mat4_mul_mat4(bench: &mut criterion::Criterion) { |
| 70 |
let a = DMatrix::<f64>::new_random(4, 4); |
| 71 |
let b = DMatrix::<f64>::new_random(4, 4); |
| 72 |
|
| 73 |
bench.bench_function("mat4_mul_mat4", move |bh| bh.iter(|| &a * &b)); |
| 74 |
} |
| 75 |
|
| 76 |
fn mat5_mul_mat5(bench: &mut criterion::Criterion) { |
| 77 |
let a = DMatrix::<f64>::new_random(5, 5); |
| 78 |
let b = DMatrix::<f64>::new_random(5, 5); |
| 79 |
|
| 80 |
bench.bench_function("mat5_mul_mat5", move |bh| bh.iter(|| &a * &b)); |
| 81 |
} |
| 82 |
|
| 83 |
fn mat6_mul_mat6(bench: &mut criterion::Criterion) { |
| 84 |
let a = DMatrix::<f64>::new_random(6, 6); |
| 85 |
let b = DMatrix::<f64>::new_random(6, 6); |
| 86 |
|
| 87 |
bench.bench_function("mat6_mul_mat6", move |bh| bh.iter(|| &a * &b)); |
| 88 |
} |
| 89 |
|
| 90 |
fn mat7_mul_mat7(bench: &mut criterion::Criterion) { |
| 91 |
let a = DMatrix::<f64>::new_random(7, 7); |
| 92 |
let b = DMatrix::<f64>::new_random(7, 7); |
| 93 |
|
| 94 |
bench.bench_function("mat7_mul_mat7", move |bh| bh.iter(|| &a * &b)); |
| 95 |
} |
| 96 |
|
| 97 |
fn mat8_mul_mat8(bench: &mut criterion::Criterion) { |
| 98 |
let a = DMatrix::<f64>::new_random(8, 8); |
| 99 |
let b = DMatrix::<f64>::new_random(8, 8); |
| 100 |
|
| 101 |
bench.bench_function("mat8_mul_mat8", move |bh| bh.iter(|| &a * &b)); |
| 102 |
} |
| 103 |
|
| 104 |
fn mat9_mul_mat9(bench: &mut criterion::Criterion) { |
| 105 |
let a = DMatrix::<f64>::new_random(9, 9); |
| 106 |
let b = DMatrix::<f64>::new_random(9, 9); |
| 107 |
|
| 108 |
bench.bench_function("mat9_mul_mat9", move |bh| bh.iter(|| &a * &b)); |
| 109 |
} |
| 110 |
|
| 111 |
fn mat10_mul_mat10(bench: &mut criterion::Criterion) { |
| 112 |
let a = DMatrix::<f64>::new_random(10, 10); |
| 113 |
let b = DMatrix::<f64>::new_random(10, 10); |
| 114 |
|
| 115 |
bench.bench_function("mat10_mul_mat10", move |bh| bh.iter(|| &a * &b)); |
| 116 |
} |
| 117 |
|
| 118 |
fn mat10_mul_mat10_static(bench: &mut criterion::Criterion) { |
| 119 |
let a = OMatrix::<f64, U10, U10>::new_random(); |
| 120 |
let b = OMatrix::<f64, U10, U10>::new_random(); |
| 121 |
|
| 122 |
bench.bench_function("mat10_mul_mat10_static", move |bh| bh.iter(|| &a * &b)); |
| 123 |
} |
| 124 |
|
| 125 |
fn mat100_mul_mat100(bench: &mut criterion::Criterion) { |
| 126 |
let a = DMatrix::<f64>::new_random(100, 100); |
| 127 |
let b = DMatrix::<f64>::new_random(100, 100); |
| 128 |
|
| 129 |
bench.bench_function("mat100_mul_mat100", move |bh| bh.iter(|| &a * &b)); |
| 130 |
} |
| 131 |
|
| 132 |
fn mat500_mul_mat500(bench: &mut criterion::Criterion) { |
| 133 |
let a = DMatrix::<f64>::from_element(500, 500, 5f64); |
| 134 |
let b = DMatrix::<f64>::from_element(500, 500, 6f64); |
| 135 |
|
| 136 |
bench.bench_function("mat500_mul_mat500", move |bh| bh.iter(|| &a * &b)); |
| 137 |
} |
| 138 |
|
| 139 |
fn iter(bench: &mut criterion::Criterion) { |
| 140 |
let a = DMatrix::<f64>::new_random(1000, 1000); |
| 141 |
|
| 142 |
bench.bench_function("iter", move |bh| { |
| 143 |
bh.iter(|| { |
| 144 |
for value in a.iter() { |
| 145 |
std::hint::black_box(value); |
| 146 |
} |
| 147 |
}) |
| 148 |
}); |
| 149 |
} |
| 150 |
|
| 151 |
fn iter_rev(bench: &mut criterion::Criterion) { |
| 152 |
let a = DMatrix::<f64>::new_random(1000, 1000); |
| 153 |
|
| 154 |
bench.bench_function("iter_rev", move |bh| { |
| 155 |
bh.iter(|| { |
| 156 |
for value in a.iter().rev() { |
| 157 |
std::hint::black_box(value); |
| 158 |
} |
| 159 |
}) |
| 160 |
}); |
| 161 |
} |
| 162 |
|
| 163 |
fn copy_from(bench: &mut criterion::Criterion) { |
| 164 |
let a = DMatrix::<f64>::new_random(1000, 1000); |
| 165 |
let mut b = DMatrix::<f64>::new_random(1000, 1000); |
| 166 |
|
| 167 |
bench.bench_function("copy_from", move |bh| { |
| 168 |
bh.iter(|| { |
| 169 |
b.copy_from(&a); |
| 170 |
}) |
| 171 |
}); |
| 172 |
} |
| 173 |
|
| 174 |
fn axpy(bench: &mut criterion::Criterion) { |
| 175 |
let x = DVector::<f64>::from_element(100000, 2.0); |
| 176 |
let mut y = DVector::<f64>::from_element(100000, 3.0); |
| 177 |
let a = 42.0; |
| 178 |
|
| 179 |
bench.bench_function("axpy", move |bh| { |
| 180 |
bh.iter(|| { |
| 181 |
y.axpy(a, &x, 1.0); |
| 182 |
}) |
| 183 |
}); |
| 184 |
} |
| 185 |
|
| 186 |
fn tr_mul_to(bench: &mut criterion::Criterion) { |
| 187 |
let a = DMatrix::<f64>::new_random(1000, 1000); |
| 188 |
let b = DVector::<f64>::new_random(1000); |
| 189 |
let mut c = DVector::from_element(1000, 0.0); |
| 190 |
|
| 191 |
bench.bench_function("tr_mul_to", move |bh| bh.iter(|| a.tr_mul_to(&b, &mut c))); |
| 192 |
} |
| 193 |
|
| 194 |
fn mat_mul_mat(bench: &mut criterion::Criterion) { |
| 195 |
let a = DMatrix::<f64>::new_random(100, 100); |
| 196 |
let b = DMatrix::<f64>::new_random(100, 100); |
| 197 |
let mut ab = DMatrix::<f64>::from_element(100, 100, 0.0); |
| 198 |
|
| 199 |
bench.bench_function("mat_mul_mat", move |bh| { |
| 200 |
bh.iter(|| { |
| 201 |
std::hint::black_box(a.mul_to(&b, &mut ab)); |
| 202 |
}) |
| 203 |
}); |
| 204 |
} |
| 205 |
|
| 206 |
fn mat100_from_fn(bench: &mut criterion::Criterion) { |
| 207 |
bench.bench_function("mat100_from_fn", move |bh| { |
| 208 |
bh.iter(|| DMatrix::from_fn(100, 100, |a, b| a + b)) |
| 209 |
}); |
| 210 |
} |
| 211 |
|
| 212 |
fn mat500_from_fn(bench: &mut criterion::Criterion) { |
| 213 |
bench.bench_function("mat500_from_fn", move |bh| { |
| 214 |
bh.iter(|| DMatrix::from_fn(500, 500, |a, b| a + b)) |
| 215 |
}); |
| 216 |
} |
| 217 |
|
| 218 |
criterion_group!( |
| 219 |
matrix, |
| 220 |
mat2_mul_m, |
| 221 |
mat3_mul_m, |
| 222 |
mat4_mul_m, |
| 223 |
mat2_tr_mul_m, |
| 224 |
mat3_tr_mul_m, |
| 225 |
mat4_tr_mul_m, |
| 226 |
mat2_add_m, |
| 227 |
mat3_add_m, |
| 228 |
mat4_add_m, |
| 229 |
mat2_sub_m, |
| 230 |
mat3_sub_m, |
| 231 |
mat4_sub_m, |
| 232 |
mat2_mul_v, |
| 233 |
mat3_mul_v, |
| 234 |
mat4_mul_v, |
| 235 |
mat2_tr_mul_v, |
| 236 |
mat3_tr_mul_v, |
| 237 |
mat4_tr_mul_v, |
| 238 |
mat2_mul_s, |
| 239 |
mat3_mul_s, |
| 240 |
mat4_mul_s, |
| 241 |
mat2_div_s, |
| 242 |
mat3_div_s, |
| 243 |
mat4_div_s, |
| 244 |
mat2_inv, |
| 245 |
mat3_inv, |
| 246 |
mat4_inv, |
| 247 |
mat2_transpose, |
| 248 |
mat3_transpose, |
| 249 |
mat4_transpose, |
| 250 |
mat_div_scalar, |
| 251 |
mat100_add_mat100, |
| 252 |
mat4_mul_mat4, |
| 253 |
mat5_mul_mat5, |
| 254 |
mat6_mul_mat6, |
| 255 |
mat7_mul_mat7, |
| 256 |
mat8_mul_mat8, |
| 257 |
mat9_mul_mat9, |
| 258 |
mat10_mul_mat10, |
| 259 |
mat10_mul_mat10_static, |
| 260 |
mat100_mul_mat100, |
| 261 |
mat500_mul_mat500, |
| 262 |
iter, |
| 263 |
iter_rev, |
| 264 |
copy_from, |
| 265 |
axpy, |
| 266 |
tr_mul_to, |
| 267 |
mat_mul_mat, |
| 268 |
mat100_from_fn, |
| 269 |
mat500_from_fn, |
| 270 |
); |