]> git.lyx.org Git - features.git/commitdiff
Small Timer helper class useful for performance investigation in src/.
authorAbdelrazak Younes <younes@lyx.org>
Sat, 18 Dec 2010 18:40:21 +0000 (18:40 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Sat, 18 Dec 2010 18:40:21 +0000 (18:40 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@36940 a592a061-630c-0410-9148-cb99ea01b6c8

src/support/Timeout.cpp
src/support/Timeout.h

index 127d3bd008d6680f72987b35e2e6c80244d3b0ea..7639fa600da3d88569eb6137a5f90041d8e95098 100644 (file)
 #include <QObject>
 #include <QTimerEvent>
 
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+
 using namespace std;
 
 namespace lyx {
@@ -142,4 +146,83 @@ Timeout & Timeout::setTimeout(unsigned int msec)
 }
 
 
+struct Timer::Private
+{
+       time_t start_time;
+};
+
+
+Timer::Timer() : d(new Private)
+{
+       restart();
+}
+
+
+void Timer::restart()
+{
+       time(&d->start_time);
+}
+
+
+int Timer::elapsed() const
+{
+       time_t end_time;
+       time(&end_time);
+       double diff = difftime(end_time, d->start_time);
+       return int(diff);
+}
+
+
+string Timer::timeStr(char separator) const
+{
+       tm * timeinfo = localtime(&d->start_time);
+       // With less flexibility we could also use:
+       //strftime(buffer, 10, "%X", timeinfo);
+       ostringstream out;
+       out << setw(2) << setfill('0');
+       if (separator) {
+               out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
+                   << separator << setw(2) << setfill('0') << timeinfo->tm_min
+                   << separator << setw(2) << setfill('0') << timeinfo->tm_sec;
+       } else {
+               out << setw(2) << setfill('0') << timeinfo->tm_hour
+                   << setw(2) << setfill('0') << timeinfo->tm_min
+                   << setw(2) << setfill('0') << timeinfo->tm_sec;
+       }
+       return out.str();
+}
+
+
+string Timer::dateStr(char separator) const
+{
+       tm * timeinfo = localtime(&d->start_time);
+       // With less flexibility we could also use:
+       //res = strftime(buffer, 10, "%d%m%y", timeinfo);
+       ostringstream out;
+       out << setw(2) << setfill('0') << timeinfo->tm_mday;
+       if (separator)
+               out << separator;
+       out << setw(2) << setfill('0') << timeinfo->tm_mon;
+       if (separator)
+               out << separator;
+       out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
+       return out.str();
+}
+
+
+string Timer::toStr() const
+{
+       tm * timeinfo = localtime(&d->start_time);
+       return asctime(timeinfo);
+}
+
+
+string Timer::currentToStr()
+{
+       time_t current_time;
+       time(&current_time);
+       tm * timeinfo = localtime(&current_time);
+       return asctime(timeinfo);
+}
+
 } // namespace lyx
index 28b00333be1e09c09000cc2f3d668ed1674afa70..de3062ae73a9794b6ec701d83e5efea4044061f4 100644 (file)
@@ -64,6 +64,27 @@ private:
        unsigned int timeout_ms;
 };
 
+// Small Timer class useful for debugging and performance investigation.
+class Timer
+{
+public:
+       Timer();
+       ///
+       void restart();
+       ///
+       int elapsed() const;
+       ///
+       std::string dateStr(char separator = 0) const;
+       ///
+       std::string timeStr(char separator = 0) const;
+       ///
+       std::string toStr() const;
+       ///
+       static std::string currentToStr();
+private:
+       struct Private;
+       Private * d;
+};
 
 } // namespace lyx