-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimer.cpp
More file actions
33 lines (26 loc) · 778 Bytes
/
Timer.cpp
File metadata and controls
33 lines (26 loc) · 778 Bytes
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
/*
* Timer.cpp
*
* Created on: 21-Sep-2019
* Author: Prashant Srivastava
*/
#include "Timer.hpp"
#include <cstdio>
Timer::Timer(const char *fmtString)
: startTime(std::chrono::steady_clock::now()),
endTime(std::chrono::steady_clock::now()), fmtString(fmtString) {}
Timer::~Timer() { show(); }
void Timer::show(bool resetStartTime) {
const double durationInSec = *this;
if (resetStartTime) {
startTime = std::chrono::steady_clock::now();
}
std::printf(fmtString, durationInSec);
}
Timer::operator double() {
endTime = std::chrono::steady_clock::now();
auto elapsedTime =
std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime)
.count();
return elapsedTime / 1e6;
}