00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef MLN_UTIL_TIMER_HH
00028 # define MLN_UTIL_TIMER_HH
00029
00033
00034 # include <mln/core/concept/proxy.hh>
00035 # include <ctime>
00036
00037
00038 namespace mln
00039 {
00040
00041 namespace util
00042 {
00043
00045 class timer : public Proxy< timer >,
00046 public mln::internal::proxy_impl<float, timer>
00047 {
00048 public:
00049
00050 timer();
00051
00052
00053 timer(const timer&);
00054 void operator=(const timer&);
00055
00056 ~timer();
00057
00058 void start();
00059
00060 float stop();
00061
00062 void resume();
00063
00064 void reset();
00065
00066
00067 void restart();
00068
00069 float read() const;
00070
00071
00072 float subj_();
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 double ms() const
00084 {
00085 return double(std::clock()) * 1000.f / CLOCKS_PER_SEC;
00086 }
00087
00088 private:
00089
00090 bool running_;
00091 float start_;
00092 float time_;
00093 };
00094
00095
00096 # ifndef MLN_INCLUDE_ONLY
00097
00098 inline
00099 timer::timer()
00100 {
00101 reset();
00102 }
00103
00104 inline
00105 timer::~timer()
00106 {
00107 reset();
00108 }
00109
00110 inline
00111 void
00112 timer::start()
00113 {
00114 mln_precondition(running_ == false);
00115 start_ = float(std::clock()) / CLOCKS_PER_SEC;
00116 time_ = 0;
00117 running_ = true;
00118 }
00119
00120 inline
00121 float
00122 timer::stop()
00123 {
00124 mln_precondition(running_ == true);
00125 time_ += float(std::clock()) / CLOCKS_PER_SEC - start_;
00126 running_ = false;
00127 return time_;
00128 }
00129
00130 inline
00131 void
00132 timer::resume()
00133 {
00134 mln_precondition(running_ == false);
00135 start_ = float(std::clock()) / CLOCKS_PER_SEC;
00136 running_ = true;
00137 }
00138
00139 inline
00140 void
00141 timer::reset()
00142 {
00143 running_ = false;
00144 start_ = -1;
00145 time_ = 0;
00146 }
00147
00148 inline
00149 void
00150 timer::restart()
00151 {
00152 reset();
00153 start();
00154 }
00155
00156
00157 inline
00158 float
00159 timer::read() const
00160 {
00161 mln_precondition(start_ != -1);
00162 return running_ ?
00163 time_ + float(std::clock()) / CLOCKS_PER_SEC - start_ :
00164 time_;
00165 }
00166
00167 inline
00168 float
00169 timer::subj_()
00170 {
00171 mln_precondition(start_ != -1);
00172 return read();
00173 }
00174
00175 # endif // ! MLN_INCLUDE_ONLY
00176
00177 }
00178
00179 }
00180
00181
00182 #endif // ! MLN_UTIL_TIMER_HH