Update tick count in timer::update (#2655)

* Update tick count in timer::update

* Return ticks from restart
This commit is contained in:
cryptocode 2020-03-12 15:22:55 +01:00 committed by GitHub
commit 7348d1e717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 7 deletions

View file

@ -42,6 +42,8 @@ TEST (timer, measure_and_compare)
ASSERT_LT (t1.since_start (), 200ms);
ASSERT_GT (t1.since_start (), 10ms);
ASSERT_GE (t1.stop (), 50ms);
std::this_thread::sleep_for (50ms);
ASSERT_GT (t1.restart (), 10ms);
}
TEST (timer, cummulative_child)

View file

@ -97,12 +97,14 @@ void nano::timer<UNIT, CLOCK>::start ()
}
template <typename UNIT, typename CLOCK>
void nano::timer<UNIT, CLOCK>::restart ()
UNIT nano::timer<UNIT, CLOCK>::restart ()
{
auto current = ticks;
state = nano::timer_state::started;
begin = CLOCK::now ();
ticks = UNIT::zero ();
measurements = 0;
return current;
}
template <typename UNIT, typename CLOCK>
@ -112,20 +114,27 @@ UNIT nano::timer<UNIT, CLOCK>::pause ()
return stop ();
}
template <typename UNIT, typename CLOCK>
void nano::timer<UNIT, CLOCK>::update_ticks ()
{
auto end = CLOCK::now ();
ticks += std::chrono::duration_cast<UNIT> (end - begin);
}
template <typename UNIT, typename CLOCK>
UNIT nano::timer<UNIT, CLOCK>::stop ()
{
debug_assert (state == nano::timer_state::started);
state = nano::timer_state::stopped;
auto end = CLOCK::now ();
ticks += std::chrono::duration_cast<UNIT> (end - begin);
update_ticks ();
return ticks;
}
template <typename UNIT, typename CLOCK>
UNIT nano::timer<UNIT, CLOCK>::value () const
UNIT nano::timer<UNIT, CLOCK>::value ()
{
update_ticks ();
return ticks;
}

View file

@ -42,8 +42,9 @@ public:
/**
* Restarts the timer by setting start time to current time and resetting tick count.
* This can be called in any timer state.
* @return duration
*/
void restart ();
UNIT restart ();
/**
* Stops the timer and increases the measurement count. A timer can be started and paused
@ -59,9 +60,9 @@ public:
UNIT stop ();
/**
* Return current tick count.
* Updates and returns current tick count.
*/
UNIT value () const;
UNIT value ();
/** Returns the duration in UNIT since the timer was last started. */
UNIT since_start () const;
@ -94,5 +95,6 @@ private:
UNIT ticks{ 0 };
UNIT minimum{ UNIT::zero () };
unsigned measurements{ 0 };
void update_ticks ();
};
}