CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Utilities/Timing/interface/GenTimer.h

Go to the documentation of this file.
00001 #ifndef UTILITIES_TIMING_GENTIMER_H
00002 #define UTILITIES_TIMING_GENTIMER_H
00003 //
00004 //   V 0.0 
00005 //
00006 #include <iosfwd>
00007 
00010 template<class Time>
00011 class GenTimeInterval {
00012 public:
00013   typedef typename Time::IntervalType IntervalType;
00014   typedef typename Time::IntervalType T;
00015 
00016 public:
00017   //
00018   GenTimeInterval(IntervalType i=0) : it(i) {}
00019 
00020   //
00021   GenTimeInterval & operator =(IntervalType i) { it=i; return *this;}
00022 
00023   //
00024   operator IntervalType & () { return it;}
00025 
00026   //
00027   operator const IntervalType & () const { return it;}
00028 
00029   //
00030   const IntervalType & ticks() const  { return it;}
00031 
00032   //
00033   double seconds() const { return Time::oneTick()*it; }
00034   
00035   //
00036   double microseconds() const { return 1.e6*seconds();}
00037 
00038 private:
00039   IntervalType it;
00040 };
00041 
00042 
00043 template<class Time>
00044 std::ostream & operator<<(std::ostream & o, const GenTimeInterval<Time> & t) {
00045   return o << t.seconds() << " seconds";
00046 }
00047 
00050 template<class Time>
00051 class GenTimer {
00052 public:
00053   typedef typename Time::TimeInterval TimeInterval;
00054   typedef typename Time::IntervalType IntervalType;
00055 
00056   typedef GenTimer<Time> self;
00057 
00058 public:
00059 
00060   struct Bias {
00061     typedef GenTimer<Time> IT;
00062     double mes;
00063     IntervalType met;
00064     Bias(unsigned int n=5000) {
00065       mes=0.;
00066       met=0;
00067       if(n==0) return;
00068       IT it; 
00069       for (unsigned int i=0; i<n;i++) {
00070         it.start();it.stop();
00071       }
00072       mes = it.lap().seconds()/double(n);
00073       met = it.lap().ticks()/IntervalType(n);
00074     }
00075   };
00076 
00077   static double bias(bool insec=true, unsigned int n=5000) {
00078     static Bias it(n);
00079     return insec ? it.mes : double(it.met) ;
00080   }
00081 
00082 
00083   static double ticksInSec() { return Time::oneTick();}
00084 
00085 public:
00087   GenTimer() : elapsed(0),  running_(0), pid(0) {}
00089   GenTimer(int ipid) : elapsed(0),  running_(0), pid(ipid) {}
00090 
00092   ~GenTimer(){}
00093 
00094   //
00095   inline bool running() const { return running_>0;}
00096 
00097   //
00098   inline void reset() { 
00099     if (running()) elapsed=-Time::time(pid);
00100     else elapsed=0;
00101   }
00102 
00103   //
00104   inline void start() {
00105     running_++;
00106     if (running_==1) elapsed-=Time::time(pid);
00107   }
00108 
00109   //
00110   inline void stop()  {
00111     if (running_==1) elapsed+=Time::time(pid);
00112     if (running_>0) running_--;
00113   }
00114 
00115   inline void forceStop() {
00116     if (running_==0) return;
00117     running_ =1;
00118     stop();
00119   }
00120 
00121   //
00122   inline typename Time::TimeInterval lap() const { 
00123     if (running()) return (elapsed+Time::time(pid));
00124     return elapsed;
00125   }
00126 
00127 private:
00128 
00129   typename Time::IntervalType elapsed;
00130   int running_;
00131   int pid;
00132 
00133 };
00134 
00135 struct DummyTime {
00136 
00137   struct OneTick {
00138     OneTick() : one(1.0) {}
00139     
00140     double one;
00141   };
00142   
00143   static double oneTick() {
00144     static OneTick local;
00145     return local.one;
00146   };
00147 
00148 
00149   typedef GenTimeInterval<DummyTime> TimeInterval;
00150   typedef long long int IntervalType;
00151   typedef long long int TimeType;
00152 
00153   inline static TimeType time() { return 0;}
00154   inline static TimeType time(int) {return 0;}
00155 };
00156 
00157 typedef GenTimer<DummyTime> DummyTimer; 
00158 
00159 #endif // UTILITIES_TIMING_GENTIMER_H