All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Stopwatch.cpp
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 
23 #include "Stopwatch.h"
24 #include "Exception.h"
25 
26 #include <cstdio>
27 #include <iostream>
28 
29 /*
30 Different clocks can be used
31 
32 gettimeofday (default):
33  seems portable
34 clock_gettime (#define __CLOCK_GETTIME):
35  requires linking -lrt (on linux)
36 */
37 
38 #ifdef __CLOCK_GETTIME
39 #include <time.h>
40 #else
41 // this is the default
42 #include <sys/time.h>
43 #endif
44 
45 using namespace std;
46 
47 namespace PLMD{
48 
49 // this is needed for friend operators
50 std::ostream& operator<<(std::ostream&os,const Stopwatch&sw){
51  return sw.log(os);
52 }
53 
54 Stopwatch::Time::operator double()const{
55  return sec+0.000000001*nsec;
56 }
57 
58 Stopwatch::Time Stopwatch::Time::get(){
59  Time t;
60 #ifdef __CLOCK_GETTIME
61  timespec ts;
62  clock_gettime(CLOCK_REALTIME,&ts);
63  t.sec=ts.tv_sec;
64  t.nsec=ts.tv_nsec;
65 #else
66  timeval tv;
67  gettimeofday(&tv,NULL);
68  t.sec=tv.tv_sec;
69  t.nsec=1000*tv.tv_usec;
70 #endif
71  return t;
72 }
73 
74 void Stopwatch::Time::reset(){
75  sec=0;
76  nsec=0;
77 }
78 
79 Stopwatch::Time::Time():
80  sec(0),nsec(0) { }
81 
83  Time t(*this);
84  if(t.nsec<t2.nsec){
85  t.sec--;
86  t.nsec+=1000000000;
87  }
88  plumed_assert(t.nsec>=t2.nsec);
89  t.nsec-=t2.nsec;
90  t.sec-=t2.sec;
91  return t;
92 }
93 
95  Time &t(*this);
96  t.nsec+=t2.nsec;
97  if(t.nsec>1000000000){
98  t.nsec-=1000000000;
99  t.sec++;
100  }
101  t.sec+=t2.sec;
102  return t;
103 }
104 
106  cycles(0),running(false),paused(false) { }
107 
109  plumed_assert(!running);
110  running=true;
111  lastStart=Time::get();
112 }
113 
115  pause();
116  cycles++;
117  total+=lap;
118  if(lap>max)max=lap;
119  if(min>lap || cycles==1)min=lap;
120  lap.reset();
121 }
122 
124  lap+=Time::get()-lastStart;
125  plumed_assert(running);
126  running=false;
127 }
128 
129 void Stopwatch::start(const std::string & name){
130  watches[name].start();
131 }
132 
133 void Stopwatch::stop(const std::string & name){
134  watches[name].stop();
135 }
136 
137 void Stopwatch::pause(const std::string & name){
138  watches[name].pause();
139 }
140 
141 
142 std::ostream& Stopwatch::log(std::ostream&os)const{
143  char buffer[1000];
144  buffer[0]=0;
145  for(unsigned i=0;i<40;i++) os<<" ";
146  os<<" Cycles Total Average Minumum Maximum\n";
147  for(map<string,Watch>::const_iterator it=watches.begin();it!=watches.end();++it){
148  const Watch&t((*it).second);
149  std::string name((*it).first);
150  os<<name;
151  for(unsigned i=name.length();i<40;i++) os<<" ";
152  std::sprintf(buffer,"%12u %12.6f %12.6f %12.6f %12.6f\n", t.cycles, double(t.total), double(t.total/t.cycles), double(t.min),double(t.max));
153  os<<buffer;
154  }
155  return os;
156 }
157 
158 }
159 
160 
161 
162 
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
STL namespace.
std::ostream & operator<<(std::ostream &os, const Stopwatch &sw)
Definition: Stopwatch.cpp:50
Class to hold the value of absolute time.
Definition: Stopwatch.h:100
static Time get()
Definition: Stopwatch.cpp:58
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