Skip to content

Commit 26a50ae

Browse files
committed
Implemented non-windows version of timer.
1 parent 5a397e1 commit 26a50ae

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

include/react/common/Timing.h

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@
1414
#include <utility>
1515

1616
#if _WIN32 || _WIN64
17+
#define REACT_FIXME_CUSTOM_TIMER 1
18+
#else
19+
#define REACT_FIXME_CUSTOM_TIMER 0
20+
#endif
21+
22+
#if REACT_FIXME_CUSTOM_TIMER
1723
#include <windows.h>
1824
#else
19-
#include <ctime>
25+
#include <chrono>
2026
#endif
2127

2228
/***************************************/ REACT_IMPL_BEGIN /**************************************/
@@ -25,7 +31,7 @@
2531
/// GetPerformanceFrequency
2632
///////////////////////////////////////////////////////////////////////////////////////////////////
2733
// Todo: Initialization not thread-safe
28-
#if _WIN32 || _WIN64
34+
#if REACT_FIXME_CUSTOM_TIMER
2935
inline const LARGE_INTEGER& GetPerformanceFrequency()
3036
{
3137
static bool init = false;
@@ -87,10 +93,11 @@ template
8793
class ConditionalTimer<threshold,true>
8894
{
8995
public:
90-
#if _WIN32 || _WIN64
96+
#if REACT_FIXME_CUSTOM_TIMER
9197
using TimestampT = LARGE_INTEGER;
92-
#elif __linux__
93-
using TimestampT = long long;
98+
#else
99+
using ClockT = std::chrono::high_resolution_clock;
100+
using TimestampT = std::chrono::time_point<ClockT>;
94101
#endif
95102

96103
class ScopedTimer
@@ -123,19 +130,22 @@ class ConditionalTimer<threshold,true>
123130

124131
void endMeasure()
125132
{
133+
#if REACT_FIXME_CUSTOM_TIMER
126134
TimestampT endTime = now();
127135

128-
#if _WIN32 || _WIN64
129-
LARGE_INTEGER durationMS;
136+
LARGE_INTEGER durationUS;
130137

131-
durationMS.QuadPart = endTime.QuadPart - startTime_.QuadPart;
132-
durationMS.QuadPart *= 1000000;
133-
durationMS.QuadPart /= GetPerformanceFrequency().QuadPart;
138+
durationUS.QuadPart = endTime.QuadPart - startTime_.QuadPart;
139+
durationUS.QuadPart *= 1000000;
140+
durationUS.QuadPart /= GetPerformanceFrequency().QuadPart;
134141

135-
parent_.isThresholdExceeded_ = durationMS.QuadPart > threshold;
142+
parent_.isThresholdExceeded_ = durationUS.QuadPart > threshold;
136143
#else
137-
// TODO
138-
parent_.isThresholdExceeded_ = false;
144+
using std::chrono::duration_cast;
145+
using std::chrono::microseconds;
146+
147+
parent_.isThresholdExceeded_ =
148+
duration_cast<microseconds>(now() - startTime_).count() > threshold;
139149
#endif
140150
}
141151

@@ -145,16 +155,12 @@ class ConditionalTimer<threshold,true>
145155

146156
static TimestampT now()
147157
{
148-
#if _WIN32 || _WIN64
158+
#if REACT_FIXME_CUSTOM_TIMER
149159
TimestampT result;
150160
QueryPerformanceCounter(&result);
151161
return result;
152162
#else
153-
struct timespec ts;
154-
clock_gettime(CLOCK_REALTIME, &ts);
155-
return static_cast<long long>(1000000000UL)
156-
* static_cast<long long>(ts.tv_sec)
157-
+ static_cast<long long>(ts.tv_nsec);
163+
return ClockT::now();
158164
#endif
159165
}
160166

0 commit comments

Comments
 (0)