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 /* *************************************/
2531// / GetPerformanceFrequency
2632// /////////////////////////////////////////////////////////////////////////////////////////////////
2733// Todo: Initialization not thread-safe
28- #if _WIN32 || _WIN64
34+ #if REACT_FIXME_CUSTOM_TIMER
2935inline const LARGE_INTEGER & GetPerformanceFrequency ()
3036{
3137 static bool init = false ;
@@ -87,10 +93,11 @@ template
8793class ConditionalTimer <threshold,true >
8894{
8995public:
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