All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Stopwatch.h
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #ifndef __PLUMED_tools_Stopwatch_h
23 #define __PLUMED_tools_Stopwatch_h
24 
25 #include <string>
26 #include <map>
27 #include <iosfwd>
28 
29 namespace PLMD{
30 
31 /**
32 \ingroup TOOLBOX
33 Class implementing stopwatch to time execution.
34 
35 Each instance of this class is a container which
36 can keep track of several named stopwatches at
37 the same time. Access to the stopwatches
38 is obtained using start(), stop(), pause() methods,
39 giving as a parameter the name of the specific stopwatch.
40 Also an empty string can be used (un-named stopwatch).
41 Finally, all the times can be logged using << operator
42 
43 \verbatim
44 #include "Stopwatch.h"
45 
46 int main(){
47  Stopwatch sw;
48  sw.start();
49 
50  sw.start("initialization");
51 // do initialization ...
52  sw.stop("initialization");
53 
54  for(int i=0;i<100;i++){
55  sw.start("loop");
56 // do calculation
57  sw.stop("loop");
58  }
59 
60  sw.stop();
61  return 0;
62 }
63 
64 \endverbatim
65 
66 Using pause a stopwatch can be put on hold until
67 the next start:
68 
69 \verbatim
70 #include "Stopwatch.h"
71 
72 int main(){
73  Stopwatch sw;
74  sw.start();
75 
76  sw.start("initialization");
77 // do initialization ...
78  sw.stop("initialization");
79 
80  for(int i=0;i<100;i++){
81  sw.start("loop");
82 // do calculation
83  sw.pause("loop");
84 // here goes something that we do not want to include
85  sw.start("loop");
86 // do calculation
87  sw.stop("loop");
88  }
89 
90  sw.stop();
91  return 0;
92 }
93 
94 \endverbatim
95 
96 */
97 
98 class Stopwatch{
99 /// Class to hold the value of absolute time
100  class Time{
101  public:
102  unsigned long sec;
103 /// I store nanosecond so as to allow high resolution clocks
104 /// (even if likely time will be measured in microseconds)
105  unsigned nsec;
106  Time();
107  Time operator-(const Time&)const;
108  const Time & operator+=(const Time&);
109  operator double()const;
110  static Time get();
111  void reset();
112  };
113 /// Class to store a single stopwatch.
114 /// Class Stopwatch contains a collection of them
115  class Watch{
116  public:
117  Watch();
123  unsigned cycles;
124  bool running;
125  bool paused;
126  void start();
127  void stop();
128  void pause();
129  };
130  std::map<std::string,Watch> watches;
131  std::ostream& log(std::ostream&)const;
132 public:
133 /// Start timer named "name"
134  void start(const std::string&name);
135  void start();
136 /// Stop timer named "name"
137  void stop(const std::string&name);
138  void stop();
139 /// Pause timer named "name"
140  void pause(const std::string&name);
141  void pause();
142 /// Dump all timers on an ostream
143  friend std::ostream& operator<<(std::ostream&,const Stopwatch&);
144 };
145 
146 inline
148  start("");
149 }
150 
151 inline
153  stop("");
154 }
155 
156 inline
158  pause("");
159 }
160 
161 }
162 
163 
164 #endif
Time operator-(const Time &) const
Definition: Stopwatch.cpp:82
unsigned long sec
Definition: Stopwatch.h:102
Class to store a single stopwatch.
Definition: Stopwatch.h:115
friend std::ostream & operator<<(std::ostream &, const Stopwatch &)
Dump all timers on an ostream.
Definition: Stopwatch.cpp:50
Class to hold the value of absolute time.
Definition: Stopwatch.h:100
unsigned nsec
I store nanosecond so as to allow high resolution clocks (even if likely time will be measured in mic...
Definition: Stopwatch.h:105
std::ostream & log(std::ostream &) const
Definition: Stopwatch.cpp:142
const Time & operator+=(const Time &)
Definition: Stopwatch.cpp:94
Class implementing stopwatch to time execution.
Definition: Stopwatch.h:98
std::map< std::string, Watch > watches
Definition: Stopwatch.h:130