Skip to content

Commit e0f5b8f

Browse files
committed
Improved accuracy of timing functions.
1 parent 3bb4511 commit e0f5b8f

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

test/TestClass.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,14 @@ class TestClass
533533

534534
++m_timed_function_calls;
535535

536-
return m_current_time > stop_time;
536+
bool complete = m_current_time > stop_time;
537+
if (complete)
538+
{
539+
unsigned long call_time = TestTime::GetCallTimeInNanoseconds();
540+
m_current_time -= TestTime(call_time * static_cast<unsigned long>(m_timed_function_calls));
541+
}
542+
543+
return complete;
537544
}
538545

539546
/// While the current time has not reached the start time + sample time,

test/TestTime.hpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class TestTime
6666
SetTime(seconds, nanoseconds);
6767
}
6868

69+
TestTime(unsigned long nanoseconds)
70+
: m_nanoseconds(nanoseconds)
71+
{
72+
}
73+
6974
TestTime(TestTime const& other_time) throw()
7075
: m_nanoseconds(other_time.m_nanoseconds)
7176
{
@@ -101,6 +106,12 @@ class TestTime
101106
return m_nanoseconds >= other_time.m_nanoseconds;
102107
}
103108

109+
TestTime& operator -= (TestTime const& time_to_subtract) throw()
110+
{
111+
m_nanoseconds -= time_to_subtract.m_nanoseconds;
112+
return *this;
113+
}
114+
104115
TestTime& operator +=(TestTime const& time_to_add) throw()
105116
{
106117
m_nanoseconds += time_to_add.m_nanoseconds;
@@ -138,7 +149,7 @@ class TestTime
138149
/// Update with the current system time.
139150
void Refresh()
140151
{
141-
clock_t ticks = clock();
152+
clock_t ticks = ::clock();
142153
privateFromClock(ticks, CLOCKS_PER_SEC, m_nanoseconds);
143154
}
144155

@@ -222,18 +233,37 @@ class TestTime
222233
m_nanoseconds = nanoseconds;
223234
}
224235

225-
void GetDiffTime(TestTime const& other_time,
236+
void GetDiffTime(TestTime const& start_time,
226237
TestTime& diff_time) const throw()
227238
{
228-
if (m_nanoseconds > other_time.m_nanoseconds)
229-
diff_time.m_nanoseconds = m_nanoseconds - other_time.m_nanoseconds;
239+
if (m_nanoseconds > start_time.m_nanoseconds)
240+
diff_time.m_nanoseconds = m_nanoseconds - start_time.m_nanoseconds;
230241
else
231-
diff_time.m_nanoseconds = other_time.m_nanoseconds - m_nanoseconds;
242+
diff_time.m_nanoseconds = 0;
232243
}
233244

234245
// Static helper member functions,
235246
// with conversion functions added to aid readability of code.
236247
public:
248+
static unsigned long GetCallTimeInNanoseconds(unsigned long sample_in_milliseconds = 10UL)
249+
{
250+
static unsigned long call_time_in_nanoseconds = 0UL;
251+
if (call_time_in_nanoseconds == 0UL)
252+
{
253+
clock_t start = ::clock();
254+
clock_t elapsed_milliseconds = 0;
255+
clock_t count = 1; // start with at least the one call for initial clock.
256+
while (static_cast<unsigned long>(elapsed_milliseconds) < sample_in_milliseconds)
257+
{
258+
clock_t elapsed = ::clock() - start;
259+
elapsed_milliseconds = (elapsed * CLOCKS_PER_SEC) / 1000;
260+
++count;
261+
}
262+
call_time_in_nanoseconds = static_cast<unsigned long>((elapsed_milliseconds * 1000000) / sample_in_milliseconds / count);
263+
}
264+
return call_time_in_nanoseconds;
265+
}
266+
237267
static void Sleep(unsigned long milliseconds)
238268
{
239269
unsigned long expected_seconds = milliseconds / MILLISECONDS_PER_SECOND;

0 commit comments

Comments
 (0)