Heidelberg Educational Numerics Library Version 0.24 (from 9 September 2011)
|
00001 #ifndef DUNE_TIMER_HH 00002 #define DUNE_TIMER_HH 00003 00004 #ifndef TIMER_USE_STD_CLOCK 00005 // headers for getrusage(2) 00006 #include <sys/resource.h> 00007 #endif 00008 00009 #include <ctime> 00010 00011 // headers for stderror(3) 00012 #include <cstring> 00013 00014 // access to errno in C++ 00015 #include <cerrno> 00016 00017 #include "exceptions.hh" 00018 00019 namespace hdnum { 00020 00026 class TimerError : public SystemError {} ; 00027 00028 00041 class Timer 00042 { 00043 public: 00045 Timer () throw(TimerError) 00046 { 00047 reset(); 00048 } 00049 00051 void reset() throw (TimerError) 00052 { 00053 #ifdef TIMER_USE_STD_CLOCK 00054 cstart = std::clock(); 00055 #else 00056 rusage ru; 00057 if (getrusage(RUSAGE_SELF, &ru)) 00058 HDNUM_THROW(TimerError, strerror(errno)); 00059 cstart = ru.ru_utime; 00060 #endif 00061 } 00062 00064 double elapsed () const throw (TimerError) 00065 { 00066 #ifdef TIMER_USE_STD_CLOCK 00067 return (std::clock()-cstart) / static_cast<double>(CLOCKS_PER_SEC); 00068 #else 00069 rusage ru; 00070 if (getrusage(RUSAGE_SELF, &ru)) 00071 HDNUM_THROW(TimerError, strerror(errno)); 00072 return 1.0 * (ru.ru_utime.tv_sec - cstart.tv_sec) + (ru.ru_utime.tv_usec - cstart.tv_usec) / (1000.0 * 1000.0); 00073 #endif 00074 } 00075 00076 private: 00077 #ifdef TIMER_USE_STD_CLOCK 00078 std::clock_t cstart; 00079 #else 00080 struct timeval cstart; 00081 #endif 00082 }; // end class Timer 00083 00084 } // end namespace 00085 00086 #endif