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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::time::{Duration, Instant};
use std::fmt::{self, Display};
use rayon::prelude::*;
use termion::{color, style};
#[derive(Debug, Clone)]
pub struct BenchVec {
pub inner: Vec<Duration>,
}
impl BenchVec {
pub fn new() -> Self {
Self { inner: Vec::new() }
}
pub fn from_vec(vec: &Vec<Duration>) -> Self {
Self { inner: vec.clone() }
}
pub fn push(&mut self, item: Duration) -> &mut Self {
self.inner.push(item);
self
}
pub fn append(&mut self, other: Self) -> &mut Self {
self.inner.append(&mut other.inner.clone());
self
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn sum(&self) -> Duration {
self.inner.par_iter().sum::<Duration>()
}
pub fn average(&self) -> Duration {
self.sum() / self.inner.len() as u32
}
pub fn standard_deviation(&self) -> f64 {
(self.sum().as_nanos() as f64 / (self.len() as f64 - 1f64)).sqrt()
}
pub fn compare(&self, other: Self) -> DurationDifference {
let avg1 = self.average();
let avg2 = other.average();
if avg1 > avg2 {
DurationDifference {
inner: avg1 - avg2,
positive: true,
}
} else {
DurationDifference {
inner: avg2 - avg1,
positive: false,
}
}
}
}
impl Display for BenchVec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let avg_duration = self.average();
let standard_deviation = self.standard_deviation();
write!(
f,
"{:?} (±{:.2}ns ~ {:.2}%)",
avg_duration,
standard_deviation,
(standard_deviation / avg_duration.as_nanos() as f64) * 100f64
)
}
}
#[derive(Debug, Clone)]
pub struct DurationDifference {
pub inner: Duration,
pub positive: bool,
}
impl DurationDifference {
pub fn new(left: &BenchVec, right: &BenchVec) -> Self {
let left_avg = left.average();
let right_avg = right.average();
if left_avg > right_avg {
Self {
inner: left_avg - right_avg,
positive: true,
}
} else {
Self {
inner: right_avg - left_avg,
positive: false,
}
}
}
}
impl Display for DurationDifference {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}{:?}",
if self.positive { "+" } else { "-" },
self.inner
)
}
}
pub struct Bencher {
measurements: Vec<BenchVec>,
iterations: usize,
}
const MAX_AUTO_ITERATIONS: usize = 1000000;
impl Bencher {
pub fn new() -> Self {
Self {
measurements: Vec::new(),
iterations: 100,
}
}
pub fn set_iterations(&mut self, iterations: usize) -> &mut Self {
self.iterations = iterations;
self
}
pub fn bench<T, F: FnMut() -> T>(&mut self, name: &str, mut func: F) -> &mut Self {
let mut durations = BenchVec::new();
println!(
"\n{}{}{}{}",
color::Fg(color::LightBlue),
style::Bold,
name,
style::Reset
);
if self.iterations == 0 {
let mut count = 0;
while count < MAX_AUTO_ITERATIONS {
let start = Instant::now();
func();
durations.push(start.elapsed());
if (durations.standard_deviation() / durations.average().as_nanos() as f64) < 0.01 && count > 1{
break;
}
count += 1;
}
} else {
for _ in 0..self.iterations {
let start = Instant::now();
func();
durations.push(start.elapsed());
}
}
println!("Result: {}", durations);
self.measurements.push(durations);
self
}
pub fn compare(&mut self) -> &mut Self {
if self.measurements.len() > 1 {
let left = self.measurements.get(self.measurements.len() - 1).unwrap();
let right = self.measurements.get(self.measurements.len() - 2).unwrap();
let diff = DurationDifference::new(left, right);
println!("Difference: {}", diff);
}
self
}
}