-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.hh
53 lines (47 loc) · 1.41 KB
/
timer.hh
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
48
49
50
51
52
53
///////////////////////////////////////////////////////////////////////////////
// timer.hh
//
// Timer class for code timing.
//
// This class depends only on the C++11 STL so it ought to be
// portable. It uses the std::clock() function which is precise to
// platform-dependent fractions of a second, as specified by
// CLOCKS_PER_SEC.
//
// How to use:
//
// // do slow initialization before creating a Timer
// Timer timer;
// // timer is now running, immediately run the code you want timed
// double elapsed = timer.elapsed();
// cout << "Elapsed time in seconds: " << elapsed << endl;
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
#include <cassert>
#include <chrono>
class Timer {
/*
private:
typedef std::high_resolution_clock::time_point time;
*/
public:
// Create a new Timer that is running as soon as it is created.
Timer() {
reset();
}
// Reset the timer.
void reset() {
_start = std::chrono::high_resolution_clock::now();
}
// Return the number of seconds since the timer was created, or the
// last time it was reset.
double elapsed() const {
auto end = std::chrono::high_resolution_clock::now();
assert(end >= _start);
auto time_span = std::chrono::duration_cast<std::chrono::duration<double>>(end - _start);
return time_span.count();
}
private:
std::chrono::high_resolution_clock::time_point _start;
};