TimeKeeper.h
Go to the documentation of this file.
1 #ifndef WIRECELLUTIL_TIMEKEEPER
2 #define WIRECELLUTIL_TIMEKEEPER
3 
4 #include <boost/date_time/posix_time/posix_time_types.hpp>
5 #include <vector>
6 
7 namespace WireCell {
8 
9  /** A helper class to give some time keeping.
10  *
11  * Use like
12  *
13  * TimeKeeper tk("starting");
14  * ...
15  * tk("starting long calculation....");
16  * do_long_calculation();
17  * tk("...done");
18  * ...
19  * info(tk.summary());
20  */
21  class TimeKeeper {
22  public:
23  typedef boost::posix_time::ptime ptime;
24  typedef boost::posix_time::time_duration deltat;
25  typedef std::pair<ptime, std::string> event;
26 
27  TimeKeeper(const std::string& msg = "start",
28  ptime starting_time = boost::posix_time::microsec_clock::local_time());
29  ~TimeKeeper();
30 
31  /// Return the time at which this time keeper was started.
32  ptime start_time() const;
33 
34  /// Return the time of the last event.
35  ptime last_time() const;
36 
37  /// Return the duration between the last two events.
38  deltat last_duration() const;
39 
40  /// Return the time duration between "now" and the start time.
41  deltat since(ptime now = boost::posix_time::microsec_clock::local_time()) const;
42 
43  /// Record an event.
45  std::string msg = "<tick>",
46  ptime now = boost::posix_time::microsec_clock::local_time());
47 
48  /// Return summary up to now.
49  std::string summary() const;
50 
51  /// Return event by index.
52  event operator[](int ind) const;
53 
54 
55  private:
56  /// Emit a formatted message for the given event index.
57  std::string emit(int ind) const;
58 
59 
60  std::vector< event > m_events;
61  };
62 }
63 #endif
ptime start_time() const
Return the time at which this time keeper was started.
Definition: TimeKeeper.cxx:23
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
event operator[](int ind) const
Return event by index.
Definition: TimeKeeper.cxx:41
std::string emit(int ind) const
Emit a formatted message for the given event index.
Definition: TimeKeeper.cxx:57
deltat since(ptime now=boost::posix_time::microsec_clock::local_time()) const
Return the time duration between "now" and the start time.
Definition: TimeKeeper.cxx:36
TimeKeeper(const std::string &msg="start", ptime starting_time=boost::posix_time::microsec_clock::local_time())
Definition: TimeKeeper.cxx:8
deltat last_duration() const
Return the duration between the last two events.
Definition: TimeKeeper.cxx:31
boost::posix_time::ptime ptime
Definition: TimeKeeper.h:23
Definition: Main.h:22
std::string summary() const
Return summary up to now.
Definition: TimeKeeper.cxx:48
std::pair< ptime, std::string > event
Definition: TimeKeeper.h:25
ptime last_time() const
Return the time of the last event.
Definition: TimeKeeper.cxx:27
std::vector< event > m_events
Definition: TimeKeeper.h:60
std::string operator()(std::string msg="<tick>", ptime now=boost::posix_time::microsec_clock::local_time())
Record an event.
Definition: TimeKeeper.cxx:17
boost::posix_time::time_duration deltat
Definition: TimeKeeper.h:24