Stopwatch.h

00001 /*************************************************************************************************
00002  *
00003  * Modeling and animation (TNM079) 2007
00004  * Code base for lab assignments. Copyright:
00005  *   Gunnar Johansson (gunnar.johansson@itn.liu.se)
00006  *   Ken Museth (ken.museth@itn.liu.se)
00007  *   Michael Bang Nielsen (bang@daimi.au.dk)
00008  *   Ola Nilsson (ola.nilsson@itn.liu.se)
00009  *   Andreas Söderström (andreas.soderstrom@itn.liu.se)
00010  *
00011  *************************************************************************************************/
00012 #ifndef _STOPWATCH_H
00013 #define _STOPWATCH_H
00014 
00015 #ifndef WIN32
00016   #include <sys/time.h>
00017 #else
00018   #include <time.h>
00019 #endif
00020 
00021 //-----------------------------------------------------------------------------
00022 inline static double seconds(bool highPrecision)
00023 {
00024   // High precision routine (wraps around after ~72 hours)
00025   if (highPrecision){ 
00026     #ifdef __linux
00027       struct timeval  tv;
00028       struct timezone tz;    
00029       gettimeofday( &tv, &tz); 
00030       return (double)(tv.tv_usec)*1.0e-6 + (double)(tv.tv_sec);
00031     #else
00032       const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
00033       return ( (double) clock() ) * secs_per_tick;
00034     #endif
00035   }
00036   else{
00037     // Low precision routine (counts seconds)
00038     return (double)(time(NULL));
00039   }
00040 }
00041 
00042 //-----------------------------------------------------------------------------
00043 class Stopwatch {
00044 private:
00045   bool mHighPrecision;
00046   int running_;
00047   double start_time_;
00048   double total_;
00049   
00050 public:
00051   inline Stopwatch();
00052   
00053   inline void highPrecision(bool p) {mHighPrecision = p;}
00054   inline void start();
00055   inline double stop();
00056   inline double read();
00057   inline void resume(bool setStartTime = true);
00058   inline int running();
00059 };
00060 
00061 //-----------------------------------------------------------------------------
00062 inline Stopwatch::Stopwatch() : mHighPrecision(true), running_(0), start_time_(0), total_(0) {}
00063 
00064 //-----------------------------------------------------------------------------
00065 inline void Stopwatch::start() {
00066   running_ = 1;
00067   total_ = 0;
00068   start_time_ = seconds(mHighPrecision);
00069 }
00070 
00071 //-----------------------------------------------------------------------------
00072 inline double Stopwatch::stop() {
00073   if (running_) {
00074     total_ += (seconds(mHighPrecision) - start_time_);
00075     running_ = 0;
00076   }
00077   return total_; 
00078 }
00079 
00080 //-----------------------------------------------------------------------------
00081 inline void Stopwatch::resume(bool setStartTime) {
00082   if (!running_) {
00083       {
00084         start_time_ = seconds(mHighPrecision);
00085       }
00086       running_ = 1;
00087   } 
00088 }
00089 
00090 //-----------------------------------------------------------------------------
00091 inline double Stopwatch::read() {
00092   if (running_) {
00093     stop();
00094     resume(false);
00095   }
00096   return total_;
00097 }
00098 
00099 
00100 #endif

Generated on Fri Jul 20 23:57:42 2007 for HalfEdge by  doxygen 1.5.1