$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mln/util/timer.hh
1 // Copyright (C) 2008, 2009, 2011, 2013 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_UTIL_TIMER_HH
28 # define MLN_UTIL_TIMER_HH
29 
33 
34 # include <mln/core/concept/proxy.hh>
35 # include <ctime>
36 
37 
38 namespace mln
39 {
40 
41  namespace util
42  {
43 
45  class timer : public Proxy< timer >,
46  public mln::internal::proxy_impl<float, timer>
47  {
48  public:
49 
50  timer();
51 
52  ~timer();
53 
54  void start();
55 
56  float stop();
57 
58  void resume();
59 
60  void reset();
61 
62  // Restart is equivalent to reset then start.
63  void restart();
64 
65  float read() const;
66 
67  // As a proxy:
68  float subj_();
69 
70  double ms() const
71  {
72  return double(std::clock()) * 1000.f / CLOCKS_PER_SEC;
73  }
74 
75  private:
76 
77  bool running_;
78  float start_;
79  float time_;
80  };
81 
82 
83 # ifndef MLN_INCLUDE_ONLY
84 
85  inline
86  timer::timer()
87  {
88  reset();
89  }
90 
91  inline
93  {
94  reset();
95  }
96 
97  inline
98  void
99  timer::start()
100  {
101  mln_precondition(running_ == false);
102  start_ = float(std::clock()) / CLOCKS_PER_SEC;
103  time_ = 0;
104  running_ = true;
105  }
106 
107  inline
108  float
109  timer::stop()
110  {
111  mln_precondition(running_ == true);
112  time_ += float(std::clock()) / CLOCKS_PER_SEC - start_;
113  running_ = false;
114  return time_;
115  }
116 
117  inline
118  void
119  timer::resume()
120  {
121  mln_precondition(running_ == false);
122  start_ = float(std::clock()) / CLOCKS_PER_SEC;
123  running_ = true;
124  }
125 
126  inline
127  void
128  timer::reset()
129  {
130  running_ = false;
131  start_ = -1;
132  time_ = 0;
133  }
134 
135  inline
136  void
138  {
139  reset();
140  start();
141  }
142 
143 
144  inline
145  float
146  timer::read() const
147  {
148  mln_precondition(start_ != -1);
149  return running_ ?
150  time_ + float(std::clock()) / CLOCKS_PER_SEC - start_ :
151  time_;
152  }
153 
154  inline
155  float
156  timer::subj_()
157  {
158  mln_precondition(start_ != -1);
159  return read();
160  }
161 
162 # endif // ! MLN_INCLUDE_ONLY
163 
164  } // end of namespace mln::util
165 
166 } // end of namespace mln
167 
168 
169 #endif // ! MLN_UTIL_TIMER_HH