Debian Codegraph @

debian/missing-sources/nalgebra/benches/common/macros.rs

raw
1 #![macro_use]
2
3 macro_rules! bench_binop(
4 ($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
5 fn $name(bh: &mut criterion::Criterion) {
6 use rand::SeedableRng;
7 let mut rng = IsaacRng::seed_from_u64(0);
8 let a = rng.random::<$t1>();
9 let b = rng.random::<$t2>();
10
11 bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
12 a.$binop(b)
13 }));
14 }
15 }
16 );
17
18 macro_rules! bench_binop_ref(
19 ($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
20 fn $name(bh: &mut criterion::Criterion) {
21 use rand::SeedableRng;
22 let mut rng = IsaacRng::seed_from_u64(0);
23 let a = rng.random::<$t1>();
24 let b = rng.random::<$t2>();
25
26 bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
27 a.$binop(&b)
28 }));
29 }
30 }
31 );
32
33 macro_rules! bench_binop_fn(
34 ($name: ident, $t1: ty, $t2: ty, $binop: path) => {
35 fn $name(bh: &mut criterion::Criterion) {
36 use rand::SeedableRng;
37 let mut rng = IsaacRng::seed_from_u64(0);
38 let a = rng.random::<$t1>();
39 let b = rng.random::<$t2>();
40
41 bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
42 $binop(&a, &b)
43 }));
44 }
45 }
46 );
47
48 macro_rules! bench_unop_na(
49 ($name: ident, $t: ty, $unop: ident) => {
50 fn $name(bh: &mut criterion::Criterion) {
51 const LEN: usize = 1 << 13;
52
53 use rand::SeedableRng;
54 let mut rng = IsaacRng::seed_from_u64(0);
55
56 let elems: Vec<$t> = (0usize .. LEN).map(|_| rng.random::<$t>()).collect();
57 let mut i = 0;
58
59 bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
60 i = (i + 1) & (LEN - 1);
61
62 unsafe {
63 std::hint::black_box(na::$unop(elems.get_unchecked(i)))
64 }
65 }));
66 }
67 }
68 );
69
70 macro_rules! bench_unop(
71 ($name: ident, $t: ty, $unop: ident) => {
72 fn $name(bh: &mut criterion::Criterion) {
73 const LEN: usize = 1 << 13;
74
75 use rand::SeedableRng;
76 let mut rng = IsaacRng::seed_from_u64(0);
77
78 let mut elems: Vec<$t> = (0usize .. LEN).map(|_| rng.random::<$t>()).collect();
79 let mut i = 0;
80
81 bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
82 i = (i + 1) & (LEN - 1);
83
84 unsafe {
85 std::hint::black_box(elems.get_unchecked_mut(i).$unop())
86 }
87 }));
88 }
89 }
90 );
91
92 macro_rules! bench_construction(
93 ($name: ident, $constructor: path, $( $args: ident: $types: ty),*) => {
94 fn $name(bh: &mut criterion::Criterion) {
95 const LEN: usize = 1 << 13;
96
97 use rand::SeedableRng;
98 let mut rng = IsaacRng::seed_from_u64(0);
99
100 $(let $args: Vec<$types> = (0usize .. LEN).map(|_| rng.random::<$types>()).collect();)*
101 let mut i = 0;
102
103 bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
104 i = (i + 1) & (LEN - 1);
105
106 unsafe {
107 let res = $constructor($(*$args.get_unchecked(i),)*);
108 std::hint::black_box(res)
109 }
110 }));
111 }
112 }
113 );