CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GenTimer.h
Go to the documentation of this file.
1 #ifndef UTILITIES_TIMING_GENTIMER_H
2 #define UTILITIES_TIMING_GENTIMER_H
3 //
4 // V 0.0
5 //
6 #include <iosfwd>
7 
10 template<class Time>
12 public:
13  typedef typename Time::IntervalType IntervalType;
14  typedef typename Time::IntervalType T;
15 
16 public:
17  //
19 
20  //
22 
23  //
24  operator IntervalType & () { return it;}
25 
26  //
27  operator const IntervalType & () const { return it;}
28 
29  //
30  const IntervalType & ticks() const { return it;}
31 
32  //
33  double seconds() const { return Time::oneTick()*it; }
34 
35  //
36  double microseconds() const { return 1.e6*seconds();}
37 
38 private:
40 };
41 
42 
43 template<class Time>
44 std::ostream & operator<<(std::ostream & o, const GenTimeInterval<Time> & t) {
45  return o << t.seconds() << " seconds";
46 }
47 
50 template<class Time>
51 class GenTimer {
52 public:
53  typedef typename Time::TimeInterval TimeInterval;
54  typedef typename Time::IntervalType IntervalType;
55 
56  typedef GenTimer<Time> self;
57 
58 public:
59 
60  struct Bias {
61  typedef GenTimer<Time> IT;
62  double mes;
64  Bias(unsigned int n=5000) {
65  mes=0.;
66  met=0;
67  if(n==0) return;
68  IT it;
69  for (unsigned int i=0; i<n;i++) {
70  it.start();it.stop();
71  }
72  mes = it.lap().seconds()/double(n);
73  met = it.lap().ticks()/IntervalType(n);
74  }
75  };
76 
77  static double bias(bool insec=true, unsigned int n=5000) {
78  static const Bias it(n);
79  return insec ? it.mes : double(it.met) ;
80  }
81 
82 
83  static double ticksInSec() { return Time::oneTick();}
84 
85 public:
87  GenTimer() : elapsed(0), running_(0), pid(0) {}
89  GenTimer(int ipid) : elapsed(0), running_(0), pid(ipid) {}
90 
93 
94  //
95  inline bool running() const { return running_>0;}
96 
97  //
98  inline void reset() {
99  if (running()) elapsed=-Time::time(pid);
100  else elapsed=0;
101  }
102 
103  //
104  inline void start() {
105  running_++;
106  if (running_==1) elapsed-=Time::time(pid);
107  }
108 
109  //
110  inline void stop() {
111  if (running_==1) elapsed+=Time::time(pid);
112  if (running_>0) running_--;
113  }
114 
115  inline void forceStop() {
116  if (running_==0) return;
117  running_ =1;
118  stop();
119  }
120 
121  //
122  inline typename Time::TimeInterval lap() const {
123  if (running()) return (elapsed+Time::time(pid));
124  return elapsed;
125  }
126 
127 private:
128 
129  typename Time::IntervalType elapsed;
130  int running_;
131  int pid;
132 
133 };
134 
135 struct DummyTime {
136 
137  struct OneTick {
138  OneTick() : one(1.0) {}
139 
140  double one;
141  };
142 
143  static double oneTick() {
144  static const OneTick local;
145  return local.one;
146  };
147 
148 
150  typedef long long int IntervalType;
151  typedef long long int TimeType;
152 
153  inline static TimeType time() { return 0;}
154  inline static TimeType time(int) {return 0;}
155 };
156 
158 
159 #endif // UTILITIES_TIMING_GENTIMER_H
static double bias(bool insec=true, unsigned int n=5000)
Definition: GenTimer.h:77
int i
Definition: DBlmapReader.cc:9
GenTimer< Time > IT
Definition: GenTimer.h:61
GenTimer()
constructor
Definition: GenTimer.h:87
long long int IntervalType
Definition: GenTimer.h:150
GenTimeInterval< DummyTime > TimeInterval
Definition: GenTimer.h:146
Time::IntervalType T
Definition: GenTimer.h:14
static double ticksInSec()
Definition: GenTimer.h:83
double microseconds() const
Definition: GenTimer.h:36
Time::IntervalType IntervalType
Definition: GenTimer.h:54
double seconds() const
Definition: GenTimer.h:33
~GenTimer()
destructor
Definition: GenTimer.h:92
Time::TimeInterval lap() const
Definition: GenTimer.h:122
IntervalType it
Definition: GenTimer.h:39
Time::IntervalType elapsed
Definition: GenTimer.h:129
bool running() const
Definition: GenTimer.h:95
void forceStop()
Definition: GenTimer.h:115
double mes
Definition: GenTimer.h:62
const IntervalType & ticks() const
Definition: GenTimer.h:30
int running_
Definition: GenTimer.h:130
int pid
Definition: GenTimer.h:131
GenTimeInterval & operator=(IntervalType i)
Definition: GenTimer.h:21
static double oneTick()
Definition: GenTimer.h:143
void start()
Definition: GenTimer.h:104
GenTimer< DummyTime > DummyTimer
Definition: GenTimer.h:157
void reset()
Definition: GenTimer.h:98
void stop()
Definition: GenTimer.h:110
IntervalType met
Definition: GenTimer.h:63
long long int TimeType
Definition: GenTimer.h:151
Time::TimeInterval TimeInterval
Definition: GenTimer.h:53
GenTimer(int ipid)
from known context
Definition: GenTimer.h:89
Bias(unsigned int n=5000)
Definition: GenTimer.h:64
static TimeType time(int)
Definition: GenTimer.h:154
GenTimeInterval(IntervalType i=0)
Definition: GenTimer.h:18
static TimeType time()
Definition: GenTimer.h:153
Time::IntervalType IntervalType
Definition: GenTimer.h:13