spot  0.9.2
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 2 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 Spot; see the file COPYING. If not, write to the Free
22 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 // 02111-1307, USA.
24 
25 #ifndef SPOT_MISC_TIMER_HH
26 # define SPOT_MISC_TIMER_HH
27 
28 # include "misc/_config.h"
29 # include <cassert>
30 # include <iosfwd>
31 # include <string>
32 # include <map>
33 # if SPOT_HAVE_SYS_TIMES_H
34 # include <sys/times.h>
35 # endif
36 # include <ctime>
37 
38 
39 namespace spot
40 {
43 
45  struct time_info
46  {
48  : utime(0), stime(0)
49  {
50  }
51  clock_t utime;
52  clock_t stime;
53  };
54 
56  class timer
57  {
58  public:
60  : running(false)
61  {
62  }
63 
65  void
67  {
68  assert(!running);
69  running = true;
70 #ifdef SPOT_HAVE_TIMES
71  struct tms tmp;
72  times(&tmp);
73  start_.utime = tmp.tms_utime;
74  start_.stime = tmp.tms_stime;
75 #else
76  start_.utime = clock();
77 #endif
78  }
79 
81  void
82  stop()
83  {
84 #ifdef SPOT_HAVE_TIMES
85  struct tms tmp;
86  times(&tmp);
87  total_.utime += tmp.tms_utime - start_.utime;
88  total_.stime += tmp.tms_stime - start_.stime;
89 #else
90  total_.utime += clock() - start_.utime;
91 #endif
92  assert(running);
93  running = false;
94  }
95 
100  clock_t
101  utime() const
102  {
103  return total_.utime;
104  }
105 
110  clock_t
111  stime() const
112  {
113  return total_.stime;
114  }
115 
116 
118  bool
119  is_running() const
120  {
121  return running;
122  }
123 
124  protected:
127  bool running;
128  };
129 
134  class timer_map
135  {
136  public:
137 
143  void
144  start(const std::string& name)
145  {
146  item_type& it = tm[name];
147  it.first.start();
148  ++it.second;
149  }
150 
154  void
155  stop(const std::string& name)
156  {
157  tm[name].first.stop();
158  }
159 
167  void
168  cancel(const std::string& name)
169  {
170  tm_type::iterator i = tm.find(name);
171  assert(i != tm.end());
172  assert(0 < i->second.second);
173  if (0 == --i->second.second)
174  tm.erase(i);
175  }
176 
178  const spot::timer&
179  timer(const std::string& name) const
180  {
181  tm_type::const_iterator i = tm.find(name);
182  assert(i != tm.end());
183  return i->second.first;
184  }
185 
187  spot::timer&
188  timer(const std::string& name)
189  {
190  return tm[name].first;
191  }
192 
198  bool
199  empty() const
200  {
201  return tm.empty();
202  }
203 
205  std::ostream&
206  print(std::ostream& os) const;
207 
209  void
211  {
212  tm.clear();
213  }
214 
215  protected:
216  typedef std::pair<spot::timer, int> item_type;
217  typedef std::map<std::string, item_type> tm_type;
219  };
220 
222 }
223 
224 #endif // SPOT_MISC_TIMER_HH

Please comment this page and report errors about it on the RefDocComments page.
Generated on Mon Jul 2 2012 17:35:47 for spot by doxygen 1.8.1.1