Timer.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // basic timer
4 //
5 //
6 ////////////////////////////////////////////////////////////////////////
7 #ifndef __TIMER_H__
8 #define __TIMER_H__
9 
10 #include <sys/time.h>
11 #include <cstdio>
12 #include <cstdlib>
13 
14 class Timer
15 {
16  private:
17  timeval startTime;
18  timeval lastTime;
19 
20  double duration(timeval &tstart, timeval &tend)
21  {
22  long sec = tend.tv_sec - tstart.tv_sec;
23  long usec = tend.tv_usec - tstart.tv_usec;
24  double tdiff = sec + usec/1.0E+6;
25  return tdiff;
26  }
27 
28  Timer() { start(); }
29 
30  Timer(const Timer&);
31  Timer& operator=(const Timer&);
32  ~Timer(){;}
33 
34  public:
35  static Timer& GetTimer()
36  {
37  static Timer inst;
38  return inst;
39  }
40 
41  void start()
42  {
43  gettimeofday(&startTime, NULL);
44  lastTime = startTime;
45  }
46 
47  double splittime(bool _save, bool _echo = true)
48  {
49  timeval curTime;
50  gettimeofday(&curTime, NULL);
51 
52  double tdiff = duration(lastTime, curTime);
53  if(_save) lastTime = curTime;
54  if(_echo) printf("Time from last %.6f s\n", tdiff);
55  return tdiff;
56  }
57 
58  double stop(bool _echo = true)
59  {
60  timeval curTime;
61  gettimeofday(&curTime, NULL);
62 
63  double tdiff = duration(startTime, curTime);
64  if(_echo) printf("Time from start %.6f s\n", tdiff);
65  return tdiff;
66  }
67 };
68 
69 
70 #endif
Timer & operator=(const Timer &)
double splittime(bool _save, bool _echo=true)
Definition: Timer.h:47
timeval lastTime
Definition: Timer.h:18
~Timer()
Definition: Timer.h:32
timeval startTime
Definition: Timer.h:17
static Timer & GetTimer()
Definition: Timer.h:35
double stop(bool _echo=true)
Definition: Timer.h:58
Timer()
Definition: Timer.h:28
double duration(timeval &tstart, timeval &tend)
Definition: Timer.h:20
void start()
Definition: Timer.h:41
static double tdiff(const art::Timestamp &ts1, const art::Timestamp &ts2)
Definition: Timer.h:14
int gettimeofday(struct timeval *, struct timezone *)