Class Reference for E1039 Core & Analysis Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PHTimer2.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <cmath>
3 #include <unistd.h>
4 #include "PHTimer2.h"
5 using namespace std;
6 
7 PHTimer2::PHTimer2(const std::string& name)
8  : m_name (name)
9  , m_state (STOP)
10  , m_time_acc(0)
11  , m_n_cycle (0)
12 {
13  ;
14 }
15 
17 {
18  ;
19 }
20 
22 {
23  return m_n_cycle > 0 ? m_time_acc / m_n_cycle : .0;
24 }
25 
27 {
28  m_state = STOP;
29  m_time_acc = 0;
30  m_n_cycle = 0;
31 }
32 
34 {
35  if(m_state == STOP) return;
36  m_state = STOP;
37  get_clock_time(&m_time_stop);
38  m_n_cycle++;
39  m_time_acc += elapsed();
40 }
41 
43 {
44  m_state = RUN;
45  get_clock_time(&m_time_start);
46 }
47 
49 {
50  reset();
51  restart();
52 }
53 
54 void PHTimer2::print_stat(std::string header, std::ostream& os) const
55 {
56  os << header << " " << setw(25) << left << m_name << right
57  << " " << setw(15) << get_time_per_cycle() << " ms"
58  << " * " << setw( 8) << m_n_cycle
59  << " = " << setw( 8) << (int)round(m_time_acc/1000) << " s" << endl;
60 }
61 
62 void PHTimer2::test(unsigned int time_msec, std::ostream& os)
63 {
64  reset();
65  restart();
66  usleep(1000 * time_msec);
67  stop();
68  os << "PHTimer2::test(): " << m_time_acc << " for " << time_msec << endl;
69 }
70 
72 double PHTimer2::elapsed() const
73 {
74  if (m_state == RUN) return -1; // Not supported
75 
76  long diff_sec = m_time_stop.tv_sec - m_time_start.tv_sec;
77  long diff_nsec = m_time_stop.tv_nsec - m_time_start.tv_nsec;
78 
79  return 1000 * diff_sec + 1e-6 * diff_nsec;
80 }
81 
82 int PHTimer2::get_clock_time(timespec* tp)
83 {
84  return clock_gettime(CLOCK_MONOTONIC_RAW, tp);
85 }
void print_stat(std::string header=" Timer2:", std::ostream &os=std::cout) const
Definition: PHTimer2.cc:54
PHTimer2(const std::string &name="Generic Timer")
Definition: PHTimer2.cc:7
double elapsed() const
In millisecond.
Definition: PHTimer2.cc:72
void reset()
Definition: PHTimer2.cc:26
void test(unsigned int time_msec, std::ostream &os=std::cout)
Test this class by waiting for a given time (in ms).
Definition: PHTimer2.cc:62
virtual ~PHTimer2()
Definition: PHTimer2.cc:16
double get_time_per_cycle() const
Definition: PHTimer2.cc:21
void reset_and_start()
Definition: PHTimer2.cc:48
void restart()
Definition: PHTimer2.cc:42
int get_clock_time(timespec *tp)
Definition: PHTimer2.cc:82
void stop()
Definition: PHTimer2.cc:33