timer.hh

Go to the documentation of this file.
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 Dune {
00020 
00030   class TimerError : public SystemError {} ;
00031    
00032 
00043 class Timer
00044 {
00045 public:
00047         Timer () throw(TimerError)
00048         {
00049           reset();
00050         }
00051 
00053         void reset() throw (TimerError)
00054         {
00055 #ifdef TIMER_USE_STD_CLOCK
00056           cstart = std::clock();
00057 #else
00058           rusage ru;
00059           if (getrusage(RUSAGE_SELF, &ru))
00060             DUNE_THROW(TimerError, strerror(errno));
00061           cstart = ru.ru_utime;
00062 #endif
00063         }
00064 
00066         double elapsed () const throw (TimerError)
00067         {
00068 #ifdef TIMER_USE_STD_CLOCK
00069           return (std::clock()-cstart) / static_cast<double>(CLOCKS_PER_SEC);
00070 #else
00071           rusage ru;
00072           if (getrusage(RUSAGE_SELF, &ru))
00073             DUNE_THROW(TimerError, strerror(errno));
00074           return 1.0 * (ru.ru_utime.tv_sec - cstart.tv_sec) + (ru.ru_utime.tv_usec - cstart.tv_usec) / (1000.0 * 1000.0);
00075 #endif
00076         }
00077     
00078 private:
00079 #ifdef TIMER_USE_STD_CLOCK
00080   std::clock_t cstart;
00081 #else
00082   struct timeval cstart;
00083 #endif
00084 }; // end class Timer 
00085 
00088 } // end namespace
00089 
00090 #endif

Generated on Thu Apr 2 10:39:54 2009 for dune-common by  doxygen 1.5.6