spot  2.0.3
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
timer.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012, 2013, 2014, 2015 Laboratoire de
3 // Recherche et Développement de l'Epita (LRDE).
4 // Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
5 // département Systèmes Répartis Coopératifs (SRC), Université Pierre
6 // et Marie Curie.
7 //
8 // This file is part of Spot, a model checking library.
9 //
10 // Spot is free software; you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Spot is distributed in the hope that it will be useful, but WITHOUT
16 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 // License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 #pragma once
24 
25 #include <spot/misc/common.hh>
26 #include <spot/misc/_config.h>
27 #include <cassert>
28 #include <iosfwd>
29 #include <string>
30 #include <map>
31 #include <chrono>
32 #if SPOT_HAVE_SYS_TIMES_H
33 # include <sys/times.h>
34 #endif
35 #include <ctime>
36 
37 
38 namespace spot
39 {
42 
43 
45  struct stopwatch
46  {
47  protected:
48  typedef std::chrono::high_resolution_clock clock;
49  clock::time_point start_;
50  public:
52  void start()
53  {
54  start_ = clock::now();
55  }
56 
61  double stop()
62  {
63  auto t = clock::now();
64  typedef std::chrono::duration<double> seconds;
65  return std::chrono::duration_cast<seconds>(t - start_).count();
66  }
67  };
68 
69 
71  struct time_info
72  {
73  time_info()
74  : utime(0), stime(0)
75  {
76  }
77  clock_t utime;
78  clock_t stime;
79  };
80 
82  class timer
83  {
84  public:
85  timer()
86  : running(false)
87  {
88  }
89 
91  void
93  {
94  assert(!running);
95  running = true;
96 #ifdef SPOT_HAVE_TIMES
97  struct tms tmp;
98  times(&tmp);
99  start_.utime = tmp.tms_utime + tmp.tms_cutime;
100  start_.stime = tmp.tms_stime + tmp.tms_cstime;
101 #else
102  start_.utime = clock();
103 #endif
104  }
105 
107  void
109  {
110 #ifdef SPOT_HAVE_TIMES
111  struct tms tmp;
112  times(&tmp);
113  total_.utime += tmp.tms_utime + tmp.tms_cutime - start_.utime;
114  total_.stime += tmp.tms_stime + tmp.tms_cstime - start_.stime;
115 #else
116  total_.utime += clock() - start_.utime;
117 #endif
118  assert(running);
119  running = false;
120  }
121 
126  clock_t
127  utime() const
128  {
129  return total_.utime;
130  }
131 
136  clock_t
137  stime() const
138  {
139  return total_.stime;
140  }
141 
142 
144  bool
145  is_running() const
146  {
147  return running;
148  }
149 
150  protected:
151  time_info start_;
152  time_info total_;
153  bool running;
154  };
155 
160  class timer_map
161  {
162  public:
163 
169  void
170  start(const std::string& name)
171  {
172  item_type& it = tm[name];
173  it.first.start();
174  ++it.second;
175  }
176 
180  void
181  stop(const std::string& name)
182  {
183  tm[name].first.stop();
184  }
185 
193  void
194  cancel(const std::string& name)
195  {
196  tm_type::iterator i = tm.find(name);
197  assert(i != tm.end());
198  assert(0 < i->second.second);
199  if (0 == --i->second.second)
200  tm.erase(i);
201  }
202 
204  const spot::timer&
205  timer(const std::string& name) const
206  {
207  tm_type::const_iterator i = tm.find(name);
208  assert(i != tm.end());
209  return i->second.first;
210  }
211 
213  spot::timer&
214  timer(const std::string& name)
215  {
216  return tm[name].first;
217  }
218 
224  bool
225  empty() const
226  {
227  return tm.empty();
228  }
229 
231  SPOT_API std::ostream&
232  print(std::ostream& os) const;
233 
235  void
237  {
238  tm.clear();
239  }
240 
241  protected:
242  typedef std::pair<spot::timer, int> item_type;
243  typedef std::map<std::string, item_type> tm_type;
244  tm_type tm;
245  };
246 
248 }
Definition: graph.hh:32
clock_t stime() const
Return the system time of all accumulated interval.
Definition: timer.hh:137
spot::timer & timer(const std::string &name)
Return the timer name.
Definition: timer.hh:214
void start()
Marks the start if the measurement.
Definition: timer.hh:52
A map of timer, where each timer has a name.
Definition: timer.hh:160
void start(const std::string &name)
Start a timer with name name.
Definition: timer.hh:170
void stop(const std::string &name)
Stop timer name.
Definition: timer.hh:181
clock_t utime() const
Return the user time of all accumulated interval.
Definition: timer.hh:127
bool is_running() const
Whether the timer is running.
Definition: timer.hh:145
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition: timer.hh:205
void cancel(const std::string &name)
Cancel timer name.
Definition: timer.hh:194
double stop()
Returns the elapsed duration in seconds.
Definition: timer.hh:61
void start()
Start a time interval.
Definition: timer.hh:92
bool empty() const
Whether there is no timer in the map.
Definition: timer.hh:225
void stop()
Stop a time interval and update the sum of all intervals.
Definition: timer.hh:108
A simple stopwatch.
Definition: timer.hh:45
std::ostream & print(std::ostream &os) const
Format information about all timers in a table.
void reset_all()
Remove information about all timers.
Definition: timer.hh:236
A timekeeper that accumulate interval of time.
Definition: timer.hh:82
A structure to record elapsed time in clock ticks.
Definition: timer.hh:71

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Mon Jul 11 2016 09:54:34 for spot by doxygen 1.8.8