-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.hpp
47 lines (37 loc) · 1.05 KB
/
timer.hpp
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
#pragma once
#include <chrono>
#include <iostream>
struct Timer {
Timer(const std::string& name, int level = 0, bool verbose = true) : level_{level}, name_{name}, cumulated_{0.} {
if (verbose)
std::cout << "Starting " << name_ << " ";
for (int i = 0; i <= level_; i++)
std::cout << "..";
std::cout << std::endl;
start_ = std::chrono::high_resolution_clock::now();
}
~Timer() {}
auto stop() {
auto now = std::chrono::high_resolution_clock::now();
cumulated_ += now - start_;
return cumulated_;
}
void restart() {
start_ = std::chrono::high_resolution_clock::now();
}
auto count() { return cumulated_.count(); }
void display() {
for (int i = 0; i <= level_; i++)
std::cout << "..";
std::cout << " " << name_ << " done : " << count() << " ms " << std::endl;
}
void stop_and_display() {
stop();
display();
}
private:
int level_;
std::string name_;
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
std::chrono::duration<double, std::milli> cumulated_;
};