-
-
Notifications
You must be signed in to change notification settings - Fork 451
/
macros.rs
113 lines (92 loc) · 3.25 KB
/
macros.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
107
108
109
110
111
112
113
#![macro_use]
macro_rules! bench_binop(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);
let a = rng.gen::<$t1>();
let b = rng.gen::<$t2>();
bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
a.$binop(b)
}));
}
}
);
macro_rules! bench_binop_ref(
($name: ident, $t1: ty, $t2: ty, $binop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);
let a = rng.gen::<$t1>();
let b = rng.gen::<$t2>();
bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
a.$binop(&b)
}));
}
}
);
macro_rules! bench_binop_fn(
($name: ident, $t1: ty, $t2: ty, $binop: path) => {
fn $name(bh: &mut criterion::Criterion) {
use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);
let a = rng.gen::<$t1>();
let b = rng.gen::<$t2>();
bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
$binop(&a, &b)
}));
}
}
);
macro_rules! bench_unop_na(
($name: ident, $t: ty, $unop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
const LEN: usize = 1 << 13;
use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);
let elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect();
let mut i = 0;
bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
test::black_box(na::$unop(elems.get_unchecked(i)))
}
}));
}
}
);
macro_rules! bench_unop(
($name: ident, $t: ty, $unop: ident) => {
fn $name(bh: &mut criterion::Criterion) {
const LEN: usize = 1 << 13;
use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);
let mut elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect();
let mut i = 0;
bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
test::black_box(elems.get_unchecked_mut(i).$unop())
}
}));
}
}
);
macro_rules! bench_construction(
($name: ident, $constructor: path, $( $args: ident: $types: ty),*) => {
fn $name(bh: &mut criterion::Criterion) {
const LEN: usize = 1 << 13;
use rand::SeedableRng;
let mut rng = IsaacRng::seed_from_u64(0);
$(let $args: Vec<$types> = (0usize .. LEN).map(|_| rng.gen::<$types>()).collect();)*
let mut i = 0;
bh.bench_function(stringify!($name), move |bh| bh.iter(|| {
i = (i + 1) & (LEN - 1);
unsafe {
let res = $constructor($(*$args.get_unchecked(i),)*);
test::black_box(res)
}
}));
}
}
);