forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cc
64 lines (51 loc) · 1.37 KB
/
timer.cc
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
#include "timer.h"
#include <loguru.hpp>
#include <iostream>
Timer::Timer() {
Reset();
}
long long Timer::ElapsedMicroseconds() const {
std::chrono::time_point<Clock> end = Clock::now();
long long elapsed = elapsed_;
if (start_.has_value()) {
elapsed +=
std::chrono::duration_cast<std::chrono::microseconds>(end - *start_)
.count();
}
return elapsed;
}
long long Timer::ElapsedMicrosecondsAndReset() {
long long elapsed = ElapsedMicroseconds();
Reset();
return elapsed;
}
void Timer::Reset() {
start_ = Clock::now();
elapsed_ = 0;
}
void Timer::ResetAndPrint(const std::string& message) {
long long elapsed = ElapsedMicroseconds();
long long milliseconds = elapsed / 1000;
long long remaining = elapsed - milliseconds;
LOG_S(INFO) << message << " took " << milliseconds << "." << remaining
<< "ms";
Reset();
}
void Timer::Pause() {
assert(start_.has_value());
std::chrono::time_point<Clock> end = Clock::now();
long long elapsed =
std::chrono::duration_cast<std::chrono::microseconds>(end - *start_)
.count();
elapsed_ += elapsed;
start_ = nullopt;
}
void Timer::Resume() {
assert(!start_.has_value());
start_ = Clock::now();
}
ScopedPerfTimer::ScopedPerfTimer(const std::string& message)
: message_(message) {}
ScopedPerfTimer::~ScopedPerfTimer() {
timer_.ResetAndPrint(message_);
}