Bridge++  Ver. 1.1.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timer.cpp
Go to the documentation of this file.
1 
14 #include "timer.h"
15 
16 //====================================================================
18 {
19  const size_t buf_size = 1024;
20  static char buf[buf_size];
21 
22  time_t current_time;
23  struct tm *timep;
24 
25  current_time = time(NULL);
26  timep = localtime(&current_time);
27 
28  strftime(buf, buf_size, "%Y/%m/%d %H:%M:%S %z", timep);
29 
30  vout.general("Timer: timestamp: %s\n", buf);
31 }
32 
33 
34 //====================================================================
36 {
37  struct timeval t_start;
38 
39 #ifdef USE_RUSAGE
40  struct rusage ru;
41  int result = getrusage(RUSAGE_SELF, &ru);
42  t_start = ru.ru_utime;
43 #else
44  int result = gettimeofday(&t_start, 0);
45 #endif
46 
47  if (result) {
48  vout.general("Timer: warning, aquiring system clock failed.\n");
49  return;
50  }
51 
52  m_start = (double)t_start.tv_sec + t_start.tv_usec * 1.0e-6;
53 
54  is_started = true;
55  ++m_counter;
56 }
57 
58 
59 //====================================================================
61 {
62  if (!is_started) return;
63 
64  struct timeval t_end;
65 
66 #ifdef USE_RUSAGE
67  struct rusage ru;
68  int result = getrusage(RUSAGE_SELF, &ru);
69  t_end = ru.ru_utime;
70 #else
71  int result = gettimeofday(&t_end, 0);
72 #endif
73 
74  if (result) {
75  vout.general("Timer: warning, aquiring system clock failed.\n");
76  return;
77  }
78 
79  double m_end = (double)t_end.tv_sec + t_end.tv_usec * 1.0e-6;
80 
81  m_elapsed += (m_end - m_start);
82 
83  is_started = false;
84 }
85 
86 
87 //====================================================================
89 {
90  is_started = false;
91  m_elapsed = double(0);
92  m_start = double(0);
93  m_counter = 0;
94 }
95 
96 
97 //====================================================================
98 double Timer::elapsed_msec() const
99 {
100  return m_elapsed * 1.0e+3;
101 }
102 
103 
104 //====================================================================
105 unsigned long Timer::get_counter() const
106 {
107  return m_counter;
108 }