Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
mln/util/timer.hh
1 // Copyright (C) 2008, 2009, 2011 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  // Without impl.
53  timer(const timer&);
54  void operator=(const timer&);
55 
56  ~timer();
57 
58  void start();
59 
60  float stop();
61 
62  void resume();
63 
64  void reset();
65 
66  // Restart is equivalent to reset then start.
67  void restart();
68 
69  float read() const;
70 
71  // As a proxy:
72  float subj_();
73 
74 // void print() const
75 // {
76 // std::cout << running_ << ' '
77 // << start_ << ' '
78 // << std::clock() << ' '
79 // << float(std::clock()) / CLOCKS_PER_SEC << ' '
80 // << time_ << std::endl;
81 // }
82 
83  double ms() const
84  {
85  return double(std::clock()) * 1000.f / CLOCKS_PER_SEC;
86  }
87 
88  private:
89 
90  bool running_;
91  float start_;
92  float time_;
93  };
94 
95 
96 # ifndef MLN_INCLUDE_ONLY
97 
98  inline
99  timer::timer()
100  {
101  reset();
102  }
103 
104  inline
105  timer::~timer()
106  {
107  reset();
108  }
109 
110  inline
111  void
112  timer::start()
113  {
114  mln_precondition(running_ == false);
115  start_ = float(std::clock()) / CLOCKS_PER_SEC;
116  time_ = 0;
117  running_ = true;
118  }
119 
120  inline
121  float
122  timer::stop()
123  {
124  mln_precondition(running_ == true);
125  time_ += float(std::clock()) / CLOCKS_PER_SEC - start_;
126  running_ = false;
127  return time_;
128  }
129 
130  inline
131  void
132  timer::resume()
133  {
134  mln_precondition(running_ == false);
135  start_ = float(std::clock()) / CLOCKS_PER_SEC;
136  running_ = true;
137  }
138 
139  inline
140  void
141  timer::reset()
142  {
143  running_ = false;
144  start_ = -1;
145  time_ = 0;
146  }
147 
148  inline
149  void
150  timer::restart()
151  {
152  reset();
153  start();
154  }
155 
156 
157  inline
158  float
159  timer::read() const
160  {
161  mln_precondition(start_ != -1);
162  return running_ ?
163  time_ + float(std::clock()) / CLOCKS_PER_SEC - start_ :
164  time_;
165  }
166 
167  inline
168  float
169  timer::subj_()
170  {
171  mln_precondition(start_ != -1);
172  return read();
173  }
174 
175 # endif // ! MLN_INCLUDE_ONLY
176 
177  } // end of namespace mln::util
178 
179 } // end of namespace mln
180 
181 
182 #endif // ! MLN_UTIL_TIMER_HH