forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiming.h
More file actions
201 lines (165 loc) · 4.82 KB
/
Timing.h
File metadata and controls
201 lines (165 loc) · 4.82 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef REACT_COMMON_TIMING_H_INCLUDED
#define REACT_COMMON_TIMING_H_INCLUDED
#pragma once
#include "react/detail/Defs.h"
#include <utility>
#if _WIN32 || _WIN64
#define REACT_FIXME_CUSTOM_TIMER 1
#else
#define REACT_FIXME_CUSTOM_TIMER 0
#endif
#if REACT_FIXME_CUSTOM_TIMER
#include <windows.h>
#else
#include <chrono>
#endif
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// GetPerformanceFrequency
///////////////////////////////////////////////////////////////////////////////////////////////////
// Todo: Initialization not thread-safe
#if REACT_FIXME_CUSTOM_TIMER
inline const LARGE_INTEGER& GetPerformanceFrequency()
{
static bool init = false;
static LARGE_INTEGER frequency;
if (init == false)
{
QueryPerformanceFrequency(&frequency);
init = true;
}
return frequency;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ConditionalTimer
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
long long threshold,
bool is_enabled
>
class ConditionalTimer
{
public:
class ScopedTimer
{
public:
// Note:
// Count is passed by ref so it can be set later if it's not known at time of creation
ScopedTimer(const ConditionalTimer&, const size_t& count);
};
void Reset();
void ForceThresholdExceeded(bool isExceeded);
bool IsThresholdExceeded() const;
};
// Disabled
template
<
long long threshold
>
class ConditionalTimer<threshold,false>
{
public:
// Defines scoped timer that does nothing
class ScopedTimer
{
public:
ScopedTimer(const ConditionalTimer&, const size_t& count) {}
};
void Reset() {}
void ForceThresholdExceeded(bool isExceeded) {}
bool IsThresholdExceeded() const { return false; }
};
// Enabled
template
<
long long threshold
>
class ConditionalTimer<threshold,true>
{
public:
#if REACT_FIXME_CUSTOM_TIMER
using TimestampT = LARGE_INTEGER;
#else
using ClockT = std::chrono::high_resolution_clock;
using TimestampT = std::chrono::time_point<ClockT>;
#endif
class ScopedTimer
{
public:
ScopedTimer(ConditionalTimer& parent, const size_t& count) :
parent_( parent ),
count_( count )
{
if (!parent_.shouldMeasure_)
return;
startMeasure();
}
~ScopedTimer()
{
if (!parent_.shouldMeasure_)
return;
parent_.shouldMeasure_ = false;
endMeasure();
}
private:
void startMeasure()
{
startTime_ = now();
}
void endMeasure()
{
#if REACT_FIXME_CUSTOM_TIMER
TimestampT endTime = now();
LARGE_INTEGER durationUS;
durationUS.QuadPart = endTime.QuadPart - startTime_.QuadPart;
durationUS.QuadPart *= 1000000;
durationUS.QuadPart /= GetPerformanceFrequency().QuadPart;
parent_.isThresholdExceeded_ = (durationUS.QuadPart - (threshold * count_)) > 0;
#else
using std::chrono::duration_cast;
using std::chrono::microseconds;
parent_.isThresholdExceeded_ =
duration_cast<microseconds>(now() - startTime_).count() > (threshold * count_);
#endif
}
ConditionalTimer& parent_;
TimestampT startTime_;
const size_t& count_;
};
static TimestampT now()
{
#if REACT_FIXME_CUSTOM_TIMER
TimestampT result;
QueryPerformanceCounter(&result);
return result;
#else
return ClockT::now();
#endif
}
void Reset()
{
shouldMeasure_ = true;
isThresholdExceeded_ = false;
}
void ForceThresholdExceeded(bool isExceeded)
{
shouldMeasure_ = false;
isThresholdExceeded_ = isExceeded;
}
bool IsThresholdExceeded() const { return isThresholdExceeded_; }
private:
// Only measure once
bool shouldMeasure_ = true;
// Until we have measured, assume the threshold is exceeded.
// The cost of initially not parallelizing what should be parallelized is much higher
// than for the other way around.
bool isThresholdExceeded_ = true;
};
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_COMMON_TIMING_H_INCLUDED