-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
35 lines (31 loc) · 1.09 KB
/
Timer.cpp
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
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
template <typename Time = chrono::milliseconds>
class Timer
{
private:
chrono::steady_clock::time_point start;
public:
Timer() { start = chrono::high_resolution_clock::now(); }
auto elapsed() { return chrono::duration_cast<Time>(chrono::high_resolution_clock::now() - start).count(); }
void restart() { start = chrono::high_resolution_clock::now(); }
};
template <typename Time = chrono::milliseconds, typename Func>
auto TimeMe(Func toTime)
{
auto start = chrono::high_resolution_clock::now();
toTime();
auto stop = chrono::high_resolution_clock::now();
return chrono::duration_cast<Time>(stop - start).count();
}
template <typename Time = chrono::milliseconds, typename Func>
void DisplayTime(Func toTime, string message)
{
auto start = chrono::high_resolution_clock::now();
auto ret = toTime();
auto stop = chrono::high_resolution_clock::now();
cout << message << " takes " << chrono::duration_cast<Time>(stop - start).count()
<< " milliseconds and returns " << ret << endl;
}