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
pub mod zuu {
use std::cell::Cell;
use std::collections::HashMap;
use std::thread::sleep;
use std::time::{Duration, Instant};
use progress_bar::{Color, finalize_progress_bar, inc_progress_bar, init_progress_bar, print_progress_bar_info, set_progress_bar_action, Style};
use colored_truecolor::Colorize;
#[macro_export]
macro_rules! take {
($e:expr) =>
{
let mut x = Performances::new();
for (k,v) in $e.iter()
{
x.capture(k,v);
}
x.print().expect("Failed");
};
($e:expr,$t:expr) =>
{
let mut x = Performances::new();
for (k,v) in $e.iter()
{
x.capture(k,v);
}
x.sleep_time($t);
x.print().expect("Failed");
};
}
pub struct Performances {
start: Instant,
all: Cell<usize>,
callback: HashMap<String, u128>,
sleep_time: u64,
}
impl Performances {
pub fn new() -> Performances
{
println!("\n {}\n", "Calculate function execution time".magenta().bold());
Self {
start: Instant::now(),
all: Cell::new(0),
callback: HashMap::new(),
sleep_time: 500,
}
}
pub fn sleep_time(&mut self, t: u64) -> &mut Performances
{
self.sleep_time = t;
return self;
}
pub fn capture<T>(&mut self, name: &str, f: &dyn Fn() -> T) -> &mut Performances
{
self.all.set(self.all.get() + 1);
f();
let time = self.start.elapsed();
self.start = Instant::now();
self.callback.insert(String::from(name), time.as_micros());
return self;
}
pub fn print(&mut self) -> Result<(), String>
{
if self.all.get() < 1 {
return Err(String::from("No captured function founded"));
}
init_progress_bar(self.all.get());
set_progress_bar_action("Loading", Color::Blue, Style::Bold);
for test in self.callback.iter() {
sleep(Duration::from_millis(self.sleep_time));
print_progress_bar_info(test.0, format!("{} µs", String::from(test.1.to_string()).blue().bold()).as_str(), Color::Green, Style::Bold);
inc_progress_bar();
}
set_progress_bar_action("Finnish", Color::Green, Style::Bold);
finalize_progress_bar();
Ok(())
}
}
}