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
use std::fmt::{Display, Formatter, Result};
use std::ops::Add;
use std::ops::Sub;
use std::time::Instant;
#[derive(Clone)]
pub struct Benchmark {
blend_time: f64,
read_png_time: f64,
write_png_time: f64,
}
impl Benchmark {
pub fn new() -> Self {
Benchmark {
blend_time: 0.0,
read_png_time: 0.0,
write_png_time: 0.0,
}
}
pub fn total(&self) -> f64 {
self.blend_time + self.read_png_time + self.write_png_time
}
pub fn execute<F, T, H>(&mut self, update_fn: H, target_fn: F) -> T
where
F: FnOnce() -> T,
H: FnOnce(&mut Self, f64),
{
let start = Instant::now();
let result = target_fn();
let duration = start.elapsed().as_micros() as f64;
update_fn(self, duration / 1000.0);
result
}
pub fn add_blend_time(benchmark: &mut Benchmark, blend_time: f64) {
benchmark.blend_time += blend_time;
}
pub fn add_read_png_time(benchmark: &mut Benchmark, read_png_time: f64) {
benchmark.read_png_time += read_png_time;
}
pub fn add_write_png_time(benchmark: &mut Benchmark, write_png_time: f64) {
benchmark.write_png_time += write_png_time;
}
}
impl Default for Benchmark {
fn default() -> Self {
Benchmark::new()
}
}
impl Display for Benchmark {
fn fmt(&self, fmt: &mut Formatter) -> Result {
fmt.write_str(&format!(
"{:.2}ms (blend {:.2}ms, read {:.2}ms, write {:.2}ms)",
self.total(),
self.blend_time,
self.read_png_time,
self.write_png_time
))?;
Ok(())
}
}
impl Add for Benchmark {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
blend_time: self.blend_time + other.blend_time,
read_png_time: self.read_png_time + other.read_png_time,
write_png_time: self.write_png_time + other.write_png_time,
}
}
}
impl Sub for Benchmark {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
blend_time: self.blend_time - other.blend_time,
read_png_time: self.read_png_time - other.read_png_time,
write_png_time: self.write_png_time - other.write_png_time,
}
}
}