Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef SPOT_MISC_TIMER_HH
00025 # define SPOT_MISC_TIMER_HH
00026
00027 # include <cassert>
00028 # include <iosfwd>
00029 # include <string>
00030 # include <map>
00031 # include <sys/times.h>
00032
00033 namespace spot
00034 {
00037
00039 struct time_info
00040 {
00041 time_info()
00042 : utime(), stime(0)
00043 {
00044 }
00045 clock_t utime;
00046 clock_t stime;
00047 };
00048
00050 class timer
00051 {
00052 public:
00053 timer()
00054 : running(false)
00055 {
00056 }
00057
00059 void
00060 start()
00061 {
00062 assert(!running);
00063 running = true;
00064 struct tms tmp;
00065 times(&tmp);
00066 start_.utime = tmp.tms_utime;
00067 start_.stime = tmp.tms_stime;
00068 }
00069
00071 void
00072 stop()
00073 {
00074 struct tms tmp;
00075 times(&tmp);
00076 total_.utime += tmp.tms_utime - start_.utime;
00077 total_.stime += tmp.tms_stime - start_.stime;
00078 assert(running);
00079 running = false;
00080 }
00081
00086 clock_t
00087 utime() const
00088 {
00089 return total_.utime;
00090 }
00091
00096 clock_t
00097 stime() const
00098 {
00099 return total_.stime;
00100 }
00101
00102
00104 bool
00105 is_running() const
00106 {
00107 return running;
00108 }
00109
00110 protected:
00111 time_info start_;
00112 time_info total_;
00113 bool running;
00114 };
00115
00120 class timer_map
00121 {
00122 public:
00123
00129 void
00130 start(const std::string& name)
00131 {
00132 item_type& it = tm[name];
00133 it.first.start();
00134 ++it.second;
00135 }
00136
00140 void
00141 stop(const std::string& name)
00142 {
00143 tm[name].first.stop();
00144 }
00145
00153 void
00154 cancel(const std::string& name)
00155 {
00156 tm_type::iterator i = tm.find(name);
00157 assert(i != tm.end());
00158 assert(0 < i->second.second);
00159 if (0 == --i->second.second)
00160 tm.erase(i);
00161 }
00162
00164 const spot::timer&
00165 timer(const std::string& name) const
00166 {
00167 tm_type::const_iterator i = tm.find(name);
00168 assert(i != tm.end());
00169 return i->second.first;
00170 }
00171
00173 spot::timer&
00174 timer(const std::string& name)
00175 {
00176 return tm[name].first;
00177 }
00178
00184 bool
00185 empty() const
00186 {
00187 return tm.empty();
00188 }
00189
00191 std::ostream&
00192 print(std::ostream& os) const;
00193
00194 protected:
00195 typedef std::pair<spot::timer, int> item_type;
00196 typedef std::map<std::string, item_type> tm_type;
00197 tm_type tm;
00198 };
00199
00201 }
00202
00203 #endif // SPOT_MISC_ESCAPE_HH