-
Notifications
You must be signed in to change notification settings - Fork 1
/
chrono.hpp
127 lines (103 loc) · 3.26 KB
/
chrono.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
#include <chrono>
#include "Essentials.hpp"
#include "WindowsInclude.h"
namespace lite
{
using std::chrono::duration;
using std::chrono::duration_cast;
using std::ratio;
using std::chrono::time_point;
// Replacement for VC++ incorrect implementation using QueryPerformanceCounter.
class high_resolution_clock
{
public: // types
typedef double rep;
typedef ratio<1, 1> period;
typedef duration<rep, period> duration;
typedef time_point<high_resolution_clock> time_point;
public: // data
// Whether the clock is non-decreasing.
static const bool is_monotonic = true;
// Whether the clock is monotonic and the time between clock ticks is constant.
// (The time between ticks is retrieved using QueryPerformanceFrequency.)
static const bool is_steady = true;
public: // methods
// Samples the current time.
static time_point now()
{
// Get the current performance counter ticks.
int64_t counter = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&counter);
// Subtract the counter from the value at program start
// so a double can hold it.
int64_t idt = counter - start_count();
double dt = static_cast<double>(idt);
WarnIf(static_cast<int64_t>(dt) != idt, "'double' cannot hold QueryPerformanceCounter delta " << idt);
return time_point(duration(dt / frequency()));
}
private: // methods
// Number of ticks per second.
static int64_t frequency()
{
static int64_t freq = -1;
if (freq == -1)
{
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
}
return freq;
}
// Counter value at the start of the application.
static int64_t start_count()
{
static int64_t count = 0;
if (count == 0)
{
QueryPerformanceCounter((LARGE_INTEGER*)&count);
}
return count;
}
};
// Stopwatch-like object which keeps track of elapsed time.
class high_resolution_timer
{
private: // data
// Time recorded when start() was last called.
high_resolution_clock::time_point startTime;
public: // methods
// Constructs timer object; calls start() if 'shouldStart' is true.
explicit high_resolution_timer(bool shouldStart = true)
{
if (shouldStart)
{
start();
}
}
// The current elapsed time as a duration object.
high_resolution_clock::duration elapsed_duration() const
{
high_resolution_clock::time_point endTime = high_resolution_clock::now();
return endTime - startTime;
}
// Microseconds that have elapsed since start() was called.
double elapsed_microseconds() const
{
return duration_cast<duration<double, micro>>(elapsed_duration()).count();
}
// Milliseconds that have elapsed since start() was called.
double elapsed_milliseconds() const
{
return duration_cast<duration<double, milli>>(elapsed_duration()).count();
}
// Seconds that have elapsed since start() was called.
double elapsed_seconds() const
{
return elapsed_duration().count();
}
// Starts or restarts the timer.
void start()
{
startTime = high_resolution_clock::now();
}
};
} // namespace lite