From 5f2fcf252b7b9790a2cb27e57f9d90ae7ea32f9e Mon Sep 17 00:00:00 2001 From: INMakrus Date: Mon, 22 Feb 2016 23:18:46 +0100 Subject: Update Time_Value.cpp - Better performance by not using a loop for normalizing time values --- ACE/ace/Time_Value.cpp | 66 +++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/ACE/ace/Time_Value.cpp b/ACE/ace/Time_Value.cpp index 36ba22b8ae2..b390d815bc4 100644 --- a/ACE/ace/Time_Value.cpp +++ b/ACE/ace/Time_Value.cpp @@ -174,52 +174,30 @@ ACE_Time_Value::dump (void) const void ACE_Time_Value::normalize (bool saturate) { - // // ACE_OS_TRACE ("ACE_Time_Value::normalize"); - // From Hans Rohnert... - - if (this->tv_.tv_usec >= ACE_ONE_SECOND_IN_USECS) - { - /*! \todo This loop needs some optimization. - */ - if (!saturate) // keep the conditionnal expression outside the while loop to minimize performance cost - do - { - ++this->tv_.tv_sec; - this->tv_.tv_usec -= ACE_ONE_SECOND_IN_USECS; - } - while (this->tv_.tv_usec >= ACE_ONE_SECOND_IN_USECS); - else - do - if (this->tv_.tv_sec < ACE_Numeric_Limits::max()) - { - ++this->tv_.tv_sec; - this->tv_.tv_usec -= ACE_ONE_SECOND_IN_USECS; - } - else - this->tv_.tv_usec = ACE_ONE_SECOND_IN_USECS - 1; - while (this->tv_.tv_usec >= ACE_ONE_SECOND_IN_USECS); - } - else if (this->tv_.tv_usec <= -ACE_ONE_SECOND_IN_USECS) + // ACE_OS_TRACE ("ACE_Time_Value::normalize"); + if (this->tv_.tv_usec >= ACE_ONE_SECOND_IN_USECS || + this->tv_.tv_usec <= -ACE_ONE_SECOND_IN_USECS) { - /*! \todo This loop needs some optimization. - */ - if (!saturate) - do - { - --this->tv_.tv_sec; - this->tv_.tv_usec += ACE_ONE_SECOND_IN_USECS; - } - while (this->tv_.tv_usec <= -ACE_ONE_SECOND_IN_USECS); + time_t sec = abs(this->tv_.tv_usec) / ACE_ONE_SECOND_IN_USECS * (this->tv_.tv_usec > 0 ? 1 : -1); + suseconds_t usec = this->tv_.tv_usec - sec * ACE_ONE_SECOND_IN_USECS; + + if (saturate && this->tv_.tv_sec > 0 && sec > 0 && + ACE_Numeric_Limits::max() - this->tv_.tv_sec < sec) + { + this->tv_.tv_sec = ACE_Numeric_Limits::max(); + this->tv_.tv_usec = ACE_ONE_SECOND_IN_USECS - 1; + } + else if (saturate && this->tv_.tv_sec < 0 && sec < 0 && + ACE_Numeric_Limits::min() - this->tv_.tv_sec > sec) + { + this->tv_.tv_sec = ACE_Numeric_Limits::min(); + this->tv_.tv_usec = -ACE_ONE_SECOND_IN_USECS + 1; + } else - do - if (this->tv_.tv_sec > ACE_Numeric_Limits::min()) - { - --this->tv_.tv_sec; - this->tv_.tv_usec += ACE_ONE_SECOND_IN_USECS; - } - else - this->tv_.tv_usec = -ACE_ONE_SECOND_IN_USECS + 1; - while (this->tv_.tv_usec <= -ACE_ONE_SECOND_IN_USECS); + { + this->tv_.tv_sec += sec; + this->tv_.tv_usec = usec; + } } if (this->tv_.tv_sec >= 1 && this->tv_.tv_usec < 0) -- cgit v1.2.1