ccls/src/timer.cc

57 lines
1.2 KiB
C++
Raw Normal View History

2017-03-25 19:18:25 +00:00
#include "timer.h"
2017-07-30 00:45:00 +00:00
#include <loguru.hpp>
2017-03-31 04:13:58 +00:00
#include <iostream>
2017-03-25 19:18:25 +00:00
Timer::Timer() {
Reset();
}
long long Timer::ElapsedMicroseconds() const {
std::chrono::time_point<Clock> end = Clock::now();
2017-10-20 20:56:26 +00:00
long long elapsed = elapsed_;
if (start_.has_value()) {
// TODO: clang-format this file.
elapsed += std::chrono::duration_cast<std::chrono::microseconds>(end - *start_).count();
}
return elapsed;
}
long long Timer::ElapsedMicrosecondsAndReset() {
2017-10-20 20:56:26 +00:00
long long elapsed = ElapsedMicroseconds();
Reset();
2017-10-20 20:56:26 +00:00
return elapsed;
}
2017-03-25 19:18:25 +00:00
void Timer::Reset() {
2017-10-20 20:56:26 +00:00
start_ = Clock::now();
elapsed_ = 0;
2017-03-25 19:18:25 +00:00
}
2017-03-31 04:13:58 +00:00
void Timer::ResetAndPrint(const std::string& message) {
2017-10-20 20:56:26 +00:00
long long elapsed = ElapsedMicroseconds();
long long milliseconds = elapsed / 1000;
long long remaining = elapsed - milliseconds;
2017-09-22 01:14:57 +00:00
LOG_S(INFO) << message << " took " << milliseconds << "." << remaining
<< "ms";
Reset();
2017-10-20 20:56:26 +00:00
}
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();
}