00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_MISC_TIMER_HH
00019 # define VCSN_MISC_TIMER_HH
00020
00026 # include <stack>
00027 # include <map>
00028 # include <list>
00029 # include <string>
00030 # include <iosfwd>
00031
00032 # define VAUCANSON 1
00033
00034 # if defined VAUCANSON
00035 # define NAMESPACE_VCSN_BEGIN namespace vcsn {
00036 # define NAMESPACE_VCSN_END } // namespace vcsn
00037 # include <vaucanson/misc/contract.hh>
00038 # else
00039 # define NAMESPACE_VCSN_BEGIN
00040 # define NAMESPACE_VCSN_END
00041 # include <cassert>
00042 # define precondition(C) assert(C)
00043 # endif
00044
00045 NAMESPACE_VCSN_BEGIN
00046
00047 namespace misc
00048 {
00049
00051 class Timer
00052 {
00053 public:
00054 friend class Bencher;
00055
00056 class TimeVar;
00057 typedef std::map<const std::string, TimeVar> task_map_type;
00058
00059 Timer ();
00060 Timer (const Timer& rhs);
00061 ~Timer ();
00062
00063 Timer operator= (const Timer& rhs);
00064
00067 void push (const std::string& name);
00068
00071 void push (const int i);
00072
00075 void pop (const std::string& task_name);
00076
00078 void pop (const int i);
00079
00081 void pop ();
00082
00085 std::ostream& print (std::ostream& o, const bool) const;
00086
00089 void print_on_destruction (std::ostream& out);
00090
00094 void name (int i, const std::string& name);
00095
00099 void start ();
00100
00103 void stop ();
00104
00109 Timer& operator<< (const Timer& rhs);
00110
00112 Timer& operator+= (const Timer& rhs);
00113
00115 Timer& operator/= (unsigned rhs);
00116
00118 Timer operator/ (unsigned rhs) const;
00119
00123 bool operator< (const Timer& rhs) const;
00124
00126 void clear();
00127
00128 TimeVar& operator[] (const std::string& s);
00129
00130 public :
00132 class Time
00133 {
00134 friend class Bencher;
00135 friend class Timer;
00136 friend class Timer::TimeVar;
00137 public:
00139 Time ();
00141 void set_to_now ();
00143 static Time now ();
00144
00146 void clear ();
00147
00151 operator bool () const;
00153 bool operator ! () const;
00155
00159 Time& operator += (const Time& rhs);
00161 Time operator + (const Time& rhs) const;
00162
00164 Time& operator -= (const Time& rhs);
00166 Time operator - (const Time& rhs) const;
00167
00169 Time& operator/= (unsigned n);
00171 Time operator/ (unsigned n) const;
00173
00177 bool operator< (const Time& rhs) const;
00178
00179 private:
00181 long user;
00183 long sys;
00185 long wall;
00186 };
00187
00189 class TimeVar
00190 {
00191 public:
00192 friend class Timer;
00193 friend class Bencher;
00194
00195 TimeVar ();
00197 void start ();
00199 void start_child ();
00201 void stop_child ();
00203 void stop ();
00204
00206 void clear ();
00207
00211 operator bool () const;
00213 bool operator ! () const;
00215
00216 TimeVar operator+= (const TimeVar& rhs);
00217 TimeVar operator+ (const TimeVar& rhs) const;
00218 TimeVar operator/= (unsigned n);
00219 TimeVar operator/ (unsigned n) const;
00220
00224 bool operator< (const TimeVar& rhs) const;
00225
00226
00227 private:
00229 Time lap ();
00230
00232 Time begin;
00233
00235 Time elapsed;
00236
00238 Time cumulated;
00239 };
00240
00242 std::ostream& print_time (long t, long total, std::ostream& o) const;
00249 std::ostream& print_time (std::string s,
00250 const Time& t, const Time& tot,
00251 std::ostream& o,
00252 const bool tree_mode) const;
00253
00254
00255 private:
00257 task_map_type tasksmap;
00258
00260 std::map<std::string, std::string> tab_to_disp;
00261
00262
00263 std::list<std::string> task_ordered;
00264
00266 std::stack<TimeVar*> tasks;
00267
00269 std::map<int, std::string> intmap;
00270
00273 TimeVar total;
00274
00276 std::ostream* dump_stream;
00277
00280 const long clocks_per_sec;
00281 };
00282
00284 std::ostream& operator<< (std::ostream& o, const Timer& t);
00285
00287 class ScopedTimer
00288 {
00289 public:
00291 ScopedTimer (Timer& timer, const std::string& key);
00293 ~ScopedTimer ();
00294 private:
00296 Timer& timer_;
00298 const std::string key_;
00299 };
00300
00301 }
00302
00303 NAMESPACE_VCSN_END
00304
00305 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
00306 # include <vaucanson/misc/timer.hxx>
00307 # if defined VAUCANSON
00308 # include <vaucanson/misc/timer.cc>
00309 # endif
00310 # endif // !VCSN_USE_INTERFACE_ONLY
00311
00312 #endif // !VCSN_MISC_TIMER_HH