spot  1.1.3
timer.hh
Go to the documentation of this file.
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012 Laboratoire de Recherche et
3 // 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 #ifndef SPOT_MISC_TIMER_HH
24 # define SPOT_MISC_TIMER_HH
25 
26 # include "misc/_config.h"
27 # include <cassert>
28 # include <iosfwd>
29 # include <string>
30 # include <map>
31 # if SPOT_HAVE_SYS_TIMES_H
32 # include <sys/times.h>
33 # endif
34 # include <ctime>
35 
36 
37 namespace spot
38 {
41 
43  struct time_info
44  {
46  : utime(0), stime(0)
47  {
48  }
49  clock_t utime;
50  clock_t stime;
51  };
52 
54  class timer
55  {
56  public:
58  : running(false)
59  {
60  }
61 
63  void
65  {
66  assert(!running);
67  running = true;
68 #ifdef SPOT_HAVE_TIMES
69  struct tms tmp;
70  times(&tmp);
71  start_.utime = tmp.tms_utime;
72  start_.stime = tmp.tms_stime;
73 #else
74  start_.utime = clock();
75 #endif
76  }
77 
79  void
80  stop()
81  {
82 #ifdef SPOT_HAVE_TIMES
83  struct tms tmp;
84  times(&tmp);
85  total_.utime += tmp.tms_utime - start_.utime;
86  total_.stime += tmp.tms_stime - start_.stime;
87 #else
88  total_.utime += clock() - start_.utime;
89 #endif
90  assert(running);
91  running = false;
92  }
93 
98  clock_t
99  utime() const
100  {
101  return total_.utime;
102  }
103 
108  clock_t
109  stime() const
110  {
111  return total_.stime;
112  }
113 
114 
116  bool
117  is_running() const
118  {
119  return running;
120  }
121 
122  protected:
125  bool running;
126  };
127 
132  class timer_map
133  {
134  public:
135 
141  void
142  start(const std::string& name)
143  {
144  item_type& it = tm[name];
145  it.first.start();
146  ++it.second;
147  }
148 
152  void
153  stop(const std::string& name)
154  {
155  tm[name].first.stop();
156  }
157 
165  void
166  cancel(const std::string& name)
167  {
168  tm_type::iterator i = tm.find(name);
169  assert(i != tm.end());
170  assert(0 < i->second.second);
171  if (0 == --i->second.second)
172  tm.erase(i);
173  }
174 
176  const spot::timer&
177  timer(const std::string& name) const
178  {
179  tm_type::const_iterator i = tm.find(name);
180  assert(i != tm.end());
181  return i->second.first;
182  }
183 
185  spot::timer&
186  timer(const std::string& name)
187  {
188  return tm[name].first;
189  }
190 
196  bool
197  empty() const
198  {
199  return tm.empty();
200  }
201 
203  std::ostream&
204  print(std::ostream& os) const;
205 
207  void
209  {
210  tm.clear();
211  }
212 
213  protected:
214  typedef std::pair<spot::timer, int> item_type;
215  typedef std::map<std::string, item_type> tm_type;
217  };
218 
220 }
221 
222 #endif // SPOT_MISC_TIMER_HH

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Tue Jul 9 2013 14:04:33 for spot by doxygen 1.8.4