-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.rs
71 lines (66 loc) · 2.31 KB
/
tools.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
use std::{self, fmt::Debug, fs, io::Write, time};
#[macro_export]
macro_rules! measuret_codeblock {
($name:literal, $($code_block:tt)+) => {
let timer = time::Instant::now();
$(
$code_block
)+
println!("measuret_codeblock('{}'): {:?}", $name, timer.elapsed());
};
}
/* `measuret_codeblock` macro: measure the execution time of some code.
* usage: `measuret_codeblock!("tag", <code to be timed>);`
* example: `measuret_codeblock!("calculate", let mut x = 1; for i in 0..10000 { x += 1; });`
* output: `measuret_codeblock('calculate'): *`.
*/
pub fn measuret_closure<T>(f: impl FnOnce() -> T) -> (T, time::Duration) {
let timer = time::Instant::now();
let result = f();
(result, timer.elapsed())
}
/* `measuret_closure<T>` function: measure the execution time of a closure.
* usage: `measuret_closure(<closure>);`
* return value: (T, std::time::Duration)
* example: `let (_, t) = measuret_closure(|| {let mut x = 0; for i in 0..10000 {x += 1;}});`
*/
pub struct Dumper {
pub path: &'static str,
pub file: std::fs::File,
}
impl Dumper {
pub fn new(path: &'static str) -> Dumper {
let file = fs::File::options()
.write(true)
.create(true)
.append(true)
.open(path)
.unwrap();
Dumper { path, file }
}
pub fn dump_line(&mut self, content: &str) {
self.file.write_fmt(format_args!("{}\n", content)).unwrap();
}
pub fn dump_debug_obj<T: Debug>(&mut self, obj: T) {
self.file.write_fmt(format_args!("{:?}\n", obj)).unwrap();
}
pub fn dump_closure_exetime<T>(&mut self, name: &str, f: impl FnOnce() -> T) -> T {
let (res, exetime) = measuret_closure(f);
self.dump_line(format!("+ '{}' exec time: {:?}", name, exetime).as_str());
res
}
pub fn dump_closure_result<T: Debug>(&mut self, name: &str, f: impl FnOnce() -> T) -> T {
let res = f();
self.dump_line(format!("+ '{}' result: {:?}", name, res).as_str());
res
}
pub fn dump_closure_exetime_result<T: Debug>(
&mut self,
name: &str,
f: impl FnOnce() -> T,
) -> T {
let (res, exetime) = measuret_closure(f);
self.dump_line(format!("+ '{}' result: {:?}; exetime: {:?}", name, res, exetime).as_str());
res
}
}