00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef SPOT_MISC_TIMER_HH
00023 # define SPOT_MISC_TIMER_HH
00024
00025 # include <cassert>
00026 # include <iosfwd>
00027 # include <string>
00028 # include <map>
00029 # include <sys/times.h>
00030
00031 namespace spot
00032 {
00035
00037 struct time_info
00038 {
00039 time_info()
00040 : utime(), stime(0)
00041 {
00042 }
00043 clock_t utime;
00044 clock_t stime;
00045 };
00046
00048 class timer
00049 {
00050 public:
00051
00053 void
00054 start()
00055 {
00056 struct tms tmp;
00057 times(&tmp);
00058 start_.utime = tmp.tms_utime;
00059 start_.stime = tmp.tms_stime;
00060 }
00061
00063 void
00064 stop()
00065 {
00066 struct tms tmp;
00067 times(&tmp);
00068 total_.utime += tmp.tms_utime - start_.utime;
00069 total_.stime += tmp.tms_stime - start_.stime;
00070 }
00071
00076 clock_t
00077 utime() const
00078 {
00079 return total_.utime;
00080 }
00081
00086 clock_t
00087 stime() const
00088 {
00089 return total_.stime;
00090 }
00091
00092 protected:
00093 time_info start_;
00094 time_info total_;
00095 };
00096
00101 class timer_map
00102 {
00103 public:
00104
00110 void
00111 start(const std::string& name)
00112 {
00113 item_type& it = tm[name];
00114 it.first.start();
00115 ++it.second;
00116 }
00117
00121 void
00122 stop(const std::string& name)
00123 {
00124 tm[name].first.stop();
00125 }
00126
00134 void
00135 cancel(const std::string& name)
00136 {
00137 tm_type::iterator i = tm.find(name);
00138 assert(i != tm.end());
00139 assert(0 < i->second.second);
00140 if (0 == --i->second.second)
00141 tm.erase(i);
00142 }
00143
00145 const spot::timer&
00146 timer(const std::string& name) const
00147 {
00148 tm_type::const_iterator i = tm.find(name);
00149 assert(i != tm.end());
00150 return i->second.first;
00151 }
00152
00154 spot::timer&
00155 timer(const std::string& name)
00156 {
00157 return tm[name].first;
00158 }
00159
00165 bool
00166 empty() const
00167 {
00168 return tm.empty();
00169 }
00170
00172 std::ostream&
00173 print(std::ostream& os) const;
00174
00175 protected:
00176 typedef std::pair<spot::timer, int> item_type;
00177 typedef std::map<std::string, item_type> tm_type;
00178 tm_type tm;
00179 };
00180
00182 }
00183
00184 #endif // SPOT_MISC_ESCAPE_HH