spot  2.1.2
 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, 2016 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  SPOT_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  SPOT_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  if (SPOT_UNLIKELY(i == tm.end()))
198  throw std::invalid_argument("timer_map::cancel(): unknown name");
199  SPOT_ASSERT(0 < i->second.second);
200  if (0 == --i->second.second)
201  tm.erase(i);
202  }
203 
205  const spot::timer&
206  timer(const std::string& name) const
207  {
208  tm_type::const_iterator i = tm.find(name);
209  if (SPOT_UNLIKELY(i == tm.end()))
210  throw std::invalid_argument("timer_map::timer(): unknown name");
211  return i->second.first;
212  }
213 
215  spot::timer&
216  timer(const std::string& name)
217  {
218  return tm[name].first;
219  }
220 
226  bool
227  empty() const
228  {
229  return tm.empty();
230  }
231 
233  SPOT_API std::ostream&
234  print(std::ostream& os) const;
235 
237  void
239  {
240  tm.clear();
241  }
242 
243  protected:
244  typedef std::pair<spot::timer, int> item_type;
245  typedef std::map<std::string, item_type> tm_type;
246  tm_type tm;
247  };
248 
250 }
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:216
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:206
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:227
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:238
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 Fri Oct 14 2016 15:38:12 for spot by doxygen 1.8.8