spot  1.99.4a
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 "common.hh"
26 #include "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  wall_start_ = std::chrono::high_resolution_clock::now();
97 #ifdef SPOT_HAVE_TIMES
98  struct tms tmp;
99  times(&tmp);
100  start_.utime = tmp.tms_utime + tmp.tms_cutime;
101  start_.stime = tmp.tms_stime + tmp.tms_cstime;
102 #else
103  start_.utime = clock();
104 #endif
105  }
106 
108  void
110  {
111  auto end = std::chrono::high_resolution_clock::now();
112  wall_cumul_ = std::chrono::duration_cast
113  <std::chrono::milliseconds>(end - wall_start_).count();
114 #ifdef SPOT_HAVE_TIMES
115  struct tms tmp;
116  times(&tmp);
117  total_.utime += tmp.tms_utime + tmp.tms_cutime - start_.utime;
118  total_.stime += tmp.tms_stime + tmp.tms_cstime - start_.stime;
119 #else
120  total_.utime += clock() - start_.utime;
121 #endif
122  assert(running);
123  running = false;
124  }
125 
130  clock_t
131  utime() const
132  {
133  return total_.utime;
134  }
135 
140  clock_t
141  stime() const
142  {
143  return total_.stime;
144  }
145 
146 
148  bool
149  is_running() const
150  {
151  return running;
152  }
153 
158  std::chrono::milliseconds::rep
159  walltime () const
160  {
161  return wall_cumul_;
162  }
163 
164  protected:
165  time_info start_;
166  time_info total_;
167  bool running;
168  std::chrono::high_resolution_clock::time_point wall_start_;
169  std::chrono::milliseconds::rep wall_cumul_;
170  };
171 
176  class timer_map
177  {
178  public:
179 
185  void
186  start(const std::string& name)
187  {
188  item_type& it = tm[name];
189  it.first.start();
190  ++it.second;
191  }
192 
196  void
197  stop(const std::string& name)
198  {
199  tm[name].first.stop();
200  }
201 
209  void
210  cancel(const std::string& name)
211  {
212  tm_type::iterator i = tm.find(name);
213  assert(i != tm.end());
214  assert(0 < i->second.second);
215  if (0 == --i->second.second)
216  tm.erase(i);
217  }
218 
220  const spot::timer&
221  timer(const std::string& name) const
222  {
223  tm_type::const_iterator i = tm.find(name);
224  assert(i != tm.end());
225  return i->second.first;
226  }
227 
229  spot::timer&
230  timer(const std::string& name)
231  {
232  return tm[name].first;
233  }
234 
240  bool
241  empty() const
242  {
243  return tm.empty();
244  }
245 
247  SPOT_API std::ostream&
248  print(std::ostream& os) const;
249 
251  void
253  {
254  tm.clear();
255  }
256 
257  protected:
258  typedef std::pair<spot::timer, int> item_type;
259  typedef std::map<std::string, item_type> tm_type;
260  tm_type tm;
261  };
262 
264 }
Definition: graph.hh:32
clock_t stime() const
Return the system time of all accumulated interval.
Definition: timer.hh:141
spot::timer & timer(const std::string &name)
Return the timer name.
Definition: timer.hh:230
std::chrono::milliseconds::rep walltime() const
Return cumulative wall time.
Definition: timer.hh:159
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:176
void start(const std::string &name)
Start a timer with name name.
Definition: timer.hh:186
void stop(const std::string &name)
Stop timer name.
Definition: timer.hh:197
clock_t utime() const
Return the user time of all accumulated interval.
Definition: timer.hh:131
bool is_running() const
Whether the timer is running.
Definition: timer.hh:149
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition: timer.hh:221
void cancel(const std::string &name)
Cancel timer name.
Definition: timer.hh:210
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:241
void stop()
Stop a time interval and update the sum of all intervals.
Definition: timer.hh:109
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:252
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 Sun Oct 11 2015 10:50:08 for spot by doxygen 1.8.9.1