Bridge++  Version 1.5.4
 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 const std::string Timer::class_name = "Timer";
17 
18 //====================================================================
20 {
21  const size_t buf_size = 1024;
22  static char buf[buf_size];
23 
24  time_t current_time;
25  struct tm *timep;
26 
27  current_time = time(NULL);
28  timep = localtime(&current_time);
29 
30  strftime(buf, buf_size, "%Y/%m/%d %H:%M:%S %z", timep);
31 
32  vout.general("%s: timestamp: %s\n", class_name.c_str(), buf);
33 }
34 
35 
36 //====================================================================
38 {
39  if (m_report_on_exit) report();
40 }
41 
42 
43 //====================================================================
45 {
46  struct timeval t_start;
47 
48 #ifdef USE_RUSAGE
49  struct rusage ru;
50  int result = getrusage(RUSAGE_SELF, &ru);
51  t_start = ru.ru_utime;
52 #else
53  int result = gettimeofday(&t_start, 0);
54 #endif
55 
56  if (result) {
57  vout.general("%s: warning, aquiring system clock failed.\n", class_name.c_str());
58  return;
59  }
60 
61  m_start = (double)t_start.tv_sec + t_start.tv_usec * 1.0e-6;
62 
63  is_started = true;
64  ++m_counter;
65 }
66 
67 
68 //====================================================================
70 {
71  if (!is_started) return;
72 
73  struct timeval t_end;
74 
75 #ifdef USE_RUSAGE
76  struct rusage ru;
77  int result = getrusage(RUSAGE_SELF, &ru);
78  t_end = ru.ru_utime;
79 #else
80  int result = gettimeofday(&t_end, 0);
81 #endif
82 
83  if (result) {
84  vout.general("%s: warning, aquiring system clock failed.\n", class_name.c_str());
85  return;
86  }
87 
88  double m_end = (double)t_end.tv_sec + t_end.tv_usec * 1.0e-6;
89 
90  m_elapsed += (m_end - m_start);
91 
92  is_started = false;
93 }
94 
95 
96 //====================================================================
98 {
99  is_started = false;
100  m_elapsed = double(0);
101  m_start = double(0);
102  m_counter = 0;
103 }
104 
105 
106 //====================================================================
107 double Timer::elapsed_sec() const
108 {
109  return m_elapsed;
110 }
111 
112 
113 //====================================================================
114 double Timer::elapsed_msec() const
115 {
116  return m_elapsed * 1.0e+3;
117 }
118 
119 
120 //====================================================================
121 unsigned long Timer::get_counter() const
122 {
123  return m_counter;
124 }
125 
126 
127 //====================================================================
129 {
130  stop();
131 
132  unsigned long count = get_counter();
133  double elapsed = elapsed_sec();
134  double average = count ? elapsed / count : 0.0;
135 
136  vout.general(vl, "Elapsed time: %s: total %12.2f sec, count %4d, average %12.2f sec\n", m_id.c_str(), elapsed, count, average);
137 }
138 
139 
140 //==========================================================
141 //==================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:503
bool m_report_on_exit
Definition: timer.h:72
double elapsed_sec() const
Definition: timer.cpp:107
void general(const char *format,...)
Definition: bridgeIO.cpp:197
void reset()
Definition: timer.cpp:97
double m_elapsed
Definition: timer.h:68
~Timer()
Definition: timer.cpp:37
bool is_started
Definition: timer.h:65
double m_start
Definition: timer.h:66
unsigned long get_counter() const
Definition: timer.cpp:121
double elapsed_msec() const
Definition: timer.cpp:114
std::string m_id
Definition: timer.h:71
unsigned long m_counter
Definition: timer.h:69
void start()
Definition: timer.cpp:44
Bridge::VerboseLevel vl
VerboseLevel
Definition: bridgeIO.h:42
static void timestamp()
Definition: timer.cpp:19
void stop()
Definition: timer.cpp:69
void report(const Bridge::VerboseLevel vl=Bridge::GENERAL)
Definition: timer.cpp:128
static const std::string class_name
Definition: timer.h:34