Jelajahi Sumber

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

	* src/TimeA2.cc
	* src/TimeA2.h
	(Time::difference): New function.
	(Time::elapsed): Done optimization.
Tatsuhiro Tsujikawa 17 tahun lalu
induk
melakukan
f771b42e53
3 mengubah file dengan 27 tambahan dan 1 penghapusan
  1. 7 0
      ChangeLog
  2. 17 1
      src/TimeA2.cc
  3. 3 0
      src/TimeA2.h

+ 7 - 0
ChangeLog

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

+ 17 - 1
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;
 }

+ 3 - 0
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;