forked from Schroedi/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiming.h
More file actions
164 lines (131 loc) · 4.15 KB
/
Timing.h
File metadata and controls
164 lines (131 loc) · 4.15 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
// 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>
#include <chrono>
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// GetPerformanceFrequency
///////////////////////////////////////////////////////////////////////////////////////////////////
// Todo: Initialization not thread-safe
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ConditionalTimer
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
std::chrono::microseconds::rep threshold,
bool is_enabled
>
class ConditionalTimer;
// Disabled
template
<
std::chrono::microseconds::rep threshold
>
class ConditionalTimer<threshold,false>
{
public:
// Defines scoped timer that does nothing
class ScopedTimer
{
public:
ScopedTimer(const ConditionalTimer&, const size_t&) {}
};
void Reset() {}
void ForceThresholdExceeded(bool) {}
bool IsThresholdExceeded() const { return false; }
};
// Enabled
template
<
std::chrono::microseconds::rep threshold
>
class ConditionalTimer<threshold,true>
{
public:
#if REACT_FIXME_CUSTOM_TIMER
using TimestampT = LARGE_INTEGER;
#else
using ClockT = std::chrono::high_resolution_clock;
using RepT = ClockT::duration::rep;
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 std::chrono::microseconds::rep& 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