From f771b42e537014eb6be0af6c007ca1714b9c5d39 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 31 May 2008 05:36:59 +0000 Subject: [PATCH] 2008-05-31 Tatsuhiro Tsujikawa * src/TimeA2.cc * src/TimeA2.h (Time::difference): New function. (Time::elapsed): Done optimization. --- ChangeLog | 7 +++++++ src/TimeA2.cc | 18 +++++++++++++++++- src/TimeA2.h | 3 +++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 2514f771..60adbe2a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-05-31 Tatsuhiro Tsujikawa + + * src/TimeA2.cc + * src/TimeA2.h + (Time::difference): New function. + (Time::elapsed): Done optimization. + 2008-05-31 Tatsuhiro Tsujikawa * src/HelpItemFactory.cc: Added missing `defined' keyword to #if diff --git a/src/TimeA2.cc b/src/TimeA2.cc index 4f029ddf..fbb0edd6 100644 --- a/src/TimeA2.cc +++ b/src/TimeA2.cc @@ -71,7 +71,18 @@ struct timeval Time::getCurrentTime() const { } bool Time::elapsed(int32_t sec) const { - return Util::difftvsec(getCurrentTime(), tv) >= sec; + // Because of gettimeofday called from getCurrentTime() is slow, and most of + // the time this function is called before specified time passes, we first do + // simple test using time. + // Then only when the further test is required, call gettimeofday. + time_t now = time(0); + if(tv.tv_sec+sec < now) { + return true; + } else if(tv.tv_sec+sec == now) { + return Util::difftvsec(getCurrentTime(), tv) >= sec; + } else { + return false; + } } bool Time::elapsedInMillis(int32_t millis) const { @@ -86,6 +97,11 @@ int32_t Time::difference() const { return Util::difftvsec(getCurrentTime(), tv); } +int32_t Time::difference(const struct timeval& now) const +{ + return Util::difftvsec(now, tv); +} + int64_t Time::differenceInMillis() const { return Util::difftv(getCurrentTime(), tv)/1000; } diff --git a/src/TimeA2.h b/src/TimeA2.h index 3f64caa2..c028ca3a 100644 --- a/src/TimeA2.h +++ b/src/TimeA2.h @@ -65,6 +65,9 @@ public: bool elapsedInMillis(int32_t millis) const; int32_t difference() const; + + int32_t difference(const struct timeval& now) const; + int64_t differenceInMillis() const; int64_t differenceInMillis(const struct timeval& now) const;