Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2008, 2009, 2011 EPITA Research and Development 00002 // Laboratory (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 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 // Without impl. 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 // Restart is equivalent to reset then start. 00067 void restart(); 00068 00069 float read() const; 00070 00071 // As a proxy: 00072 float subj_(); 00073 00074 // void print() const 00075 // { 00076 // std::cout << running_ << ' ' 00077 // << start_ << ' ' 00078 // << std::clock() << ' ' 00079 // << float(std::clock()) / CLOCKS_PER_SEC << ' ' 00080 // << time_ << std::endl; 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 } // end of namespace mln::util 00178 00179 } // end of namespace mln 00180 00181 00182 #endif // ! MLN_UTIL_TIMER_HH