-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
48 lines (37 loc) · 1.15 KB
/
util.cpp
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
#include "util.h"
std::mutex timer::l;
timer::timer(std::string label_) :
begin(std::chrono::steady_clock::now()),
label(std::move(label_)),
done(false) {}
void timer::stop() {
destroy();
}
void timer::kill() {
destroy(false);
}
timer::~timer() {
destroy();
}
std::string timer::toReadable(long ns) {
std::string suffixes[]{"ns", "µs", "ms", "s"};
auto exp = std::log10(ns);
exp = std::min(9.0, exp - std::fmod(exp, 3));
auto elapsed = static_cast<double>(ns) / pow(10, exp);
auto suffixIdx = static_cast<int>(std::min(3.0, exp / 3));
auto suffix = suffixes[suffixIdx];
std::ostringstream retStream;
retStream << std::fixed << std::setprecision(3) << elapsed << suffix;
return retStream.str();
}
void timer::destroy(bool print) {
if (!done) {
if (print) {
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
std::lock_guard<std::mutex> lock(l);
std::cout << label << ": " << toReadable(elapsed) << std::endl;
}
done = true;
}
}