-
Notifications
You must be signed in to change notification settings - Fork 22
/
timer.h
37 lines (32 loc) · 1007 Bytes
/
timer.h
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
#pragma once
#include <chrono>
template <typename TimeT = std::chrono::milliseconds> class Timer {
public:
Timer() {
start = std::chrono::system_clock::now();
}
size_t value() const {
auto now = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<TimeT>(now - start);
return (size_t) duration.count();
}
size_t reset() {
auto now = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<TimeT>(now - start);
start = now;
return (size_t) duration.count();
}
void beginStage(const std::string &name) {
reset();
std::cout << name << " .. ";
std::cout.flush();
}
void endStage(const std::string &str = "") {
std::cout << "done. (took " << value() << " ms";
if (!str.empty())
std::cout << ", " << str;
std::cout << ")" << std::endl;
}
private:
std::chrono::system_clock::time_point start;
};