00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef TVMET_UTIL_TIMER_H
00025 #define TVMET_UTIL_TIMER_H
00026
00027 #if defined(TVMET_HAVE_SYS_TIME_H) && defined(TVMET_HAVE_UNISTD_H)
00028 # include <sys/time.h>
00029 # include <sys/resource.h>
00030 # include <unistd.h>
00031 #else
00032 # include <ctime>
00033 #endif
00034
00035 namespace tvmet {
00036
00037 namespace util {
00038
00052 class Timer
00053 {
00054 Timer(const Timer&);
00055 Timer& operator=(const Timer&);
00056
00057 public:
00058 typedef double time_t;
00059
00060 public:
00062 Timer() { m_start_time = getTime(); }
00063
00065 void restart() { m_start_time = getTime(); }
00066
00068 time_t elapsed() const { return (getTime() - m_start_time); }
00069
00070 private:
00071 time_t getTime() const {
00072 #if defined(TVMET_HAVE_SYS_TIME_H) && defined(TVMET_HAVE_UNISTD_H)
00073 getrusage(RUSAGE_SELF, &m_rusage);
00074 time_t sec = m_rusage.ru_utime.tv_sec;
00075 time_t usec = m_rusage.ru_utime.tv_usec;
00076 return sec + usec/1e6;
00077 #else
00078 return static_cast<time_t>(std::clock()) / static_cast<time_t>(CLOCKS_PER_SEC);
00079 #endif
00080 }
00081
00082 private:
00083 #if defined(TVMET_HAVE_SYS_TIME_H) && defined(TVMET_HAVE_UNISTD_H)
00084 mutable struct rusage m_rusage;
00085 #endif
00086 time_t m_start_time;
00087 };
00088
00089 }
00090
00091 }
00092
00093 #endif // TVMET_UTIL_TIMER_H
00094
00095
00096
00097
00098