2008-05-31 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

* src/TimeA2.cc
	* src/TimeA2.h
	(Time::difference): New function.
	(Time::elapsed): Done optimization.
pull/1/head
Tatsuhiro Tsujikawa 2008-05-31 05:36:59 +00:00
parent 78dbc71960
commit f771b42e53
3 changed files with 27 additions and 1 deletions

View File

@ -1,3 +1,10 @@
2008-05-31 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/TimeA2.cc
* src/TimeA2.h
(Time::difference): New function.
(Time::elapsed): Done optimization.
2008-05-31 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/HelpItemFactory.cc: Added missing `defined' keyword to #if

View File

@ -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;
}

View File

@ -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;