CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TimingReport.h
Go to the documentation of this file.
1 #ifndef UTILITIES_TIMING_TIMINGREPORT_H
2 #define UTILITIES_TIMING_TIMINGREPORT_H
3 //
4 // V 1.1 01/09/2000
5 // Fast timing on Pentium
6 // on/off control introduced
7 // fixed format output
8 // V 1.2 21/02/2001
9 // cpu time added
10 // not thread safe yet...
11 
12 #include <string>
13 #include <map>
14 #include <iosfwd>
15 
19 
20 /* a class to manage Timing
21 **/
22 class TimingReport {
23 public:
25 
26  class Item {
28  public:
29  Item() : on(true), cpuon(true), counter(0), o(0){}
30  Item & switchOn(bool ion) {on=ion; return *this;}
31  Item & switchCPU(bool ion) {cpuon=ion; return *this;}
32  void start() { if (on) {counter++; if (cpuon) cpuwatch.start(); stopwatch.start(); }}
33  void stop(){
34  if (on) {
35  stopwatch.stop();
36  if (cpuon) cpuwatch.stop();
37  if (active()) return;
38  if (o) (*o)(std::pair<double,double>(stopwatch.lap().seconds(),
39  cpuwatch.lap().seconds()));
40  }
41  }
42  public:
43  bool active() const { return stopwatch.running();}
44  void setObs(MyObserver * io) { o=io;}
45  double realsec() const;
46  double realticks() const;
47  double cpusec() const;
48  public:
49  bool on;
50  bool cpuon;
51  int counter;
55 
56  };
57 
58 public:
59  static TimingReport * current();
60 
61 protected:
62 
63  typedef std::map< std::string, Item, std::less<std::string> > SMAP;
64 
65  TimingReport();
66 
67 public:
68  ~TimingReport();
69 
71  void dump(std::ostream & co, bool active=false);
72 
74  bool & inTicks() { return inTicks_;}
75 
77  void switchOn(bool ion);
78 
80  void switchOn(const std::string& name, bool ion) {
81  registry[name].switchOn(ion);
82  }
83 
84  void start(const std::string& name) {
85  if(on) registry[name].start();
86  }
87  void stop(const std::string& name) {
88  if (on) registry[name].stop();
89  }
90 
91  Item & operator[](const std::string& name) {
92  SMAP::iterator p = registry.find(name);
93  if (p!=registry.end()) return (*p).second;
94  return make(name);
95  }
96 
97  const Item & operator[](const std::string& name) const {
98  SMAP::const_iterator p = registry.find(name);
99  if (p!=registry.end()) return (*p).second;
100  return const_cast<TimingReport*>(this)->make(name);
101  }
102 
103  Item & make(const std::string& name) {
104  return registry[name].switchOn(on);
105  }
106 
107  const bool & isOn() const { return on;}
108 
109 private:
110 
111  bool on;
112  bool inTicks_;
114 
115 
116 };
117 
124 class TimeMe{
125 
126 public:
128  explicit TimeMe(const std::string& name, bool cpu=true) :
129  item((*TimingReport::current())[name]) {
130  item.switchCPU(cpu);
131  item.start();
132  }
133 
134  explicit TimeMe(TimingReport::Item & iitem, bool cpu=true) :
135  item(iitem) {
136  item.switchCPU(cpu);
137  item.start();
138  }
139 
140  std::pair<double,double> lap() const {
141  return std::pair<double,double>(item.stopwatch.lap().seconds(),
142  item.cpuwatch.lap().seconds());
143  }
144 
147  item.stop();
148  }
149 
150 private:
151 
153 
154 };
155 
156 #endif // UTILITIES_TIMING_TIMINGREPORT_H
BaseEvent< std::pair< double, double > > MyObserver
Definition: TimingReport.h:27
LinuxCPUTimer cpuwatch
Definition: TimingReport.h:53
void start(const std::string &name)
Definition: TimingReport.h:84
const bool & isOn() const
Definition: TimingReport.h:107
double realticks() const
Definition: TimingReport.cc:12
PentiumTimer stopwatch
Definition: TimingReport.h:52
void stop(const std::string &name)
Definition: TimingReport.h:87
Item & switchOn(bool ion)
Definition: TimingReport.h:30
bool & inTicks()
report in ticks
Definition: TimingReport.h:74
TimeMe(TimingReport::Item &iitem, bool cpu=true)
Definition: TimingReport.h:134
double realsec() const
Definition: TimingReport.cc:8
double seconds() const
Definition: GenTimer.h:33
Time::TimeInterval lap() const
Definition: GenTimer.h:122
bool running() const
Definition: GenTimer.h:95
std::map< std::string, Item, std::less< std::string > > SMAP
Definition: TimingReport.h:63
const Item & operator[](const std::string &name) const
Definition: TimingReport.h:97
TimeMe(const std::string &name, bool cpu=true)
Definition: TimingReport.h:128
BaseEvent< std::pair< double, double > > ItemObserver
Definition: TimingReport.h:24
bool active() const
Definition: TimingReport.h:43
void start()
Definition: GenTimer.h:104
void stop()
Definition: GenTimer.h:110
static TimingReport * current()
Definition: TimingReport.cc:21
void setObs(MyObserver *io)
Definition: TimingReport.h:44
void switchOn(const std::string &name, bool ion)
switch one ion
Definition: TimingReport.h:80
MyObserver * o
Definition: TimingReport.h:54
TimingReport::Item & item
Definition: TimingReport.h:152
Item & operator[](const std::string &name)
Definition: TimingReport.h:91
void switchOn(bool ion)
switch all on
Definition: TimingReport.cc:35
Item & switchCPU(bool ion)
Definition: TimingReport.h:31
double cpusec() const
Definition: TimingReport.cc:16
void dump(std::ostream &co, bool active=false)
Definition: TimingReport.cc:50
Item & make(const std::string &name)
Definition: TimingReport.h:103
std::pair< double, double > lap() const
Definition: TimingReport.h:140