ICP  1
icptimer.h
Go to the documentation of this file.
1 #ifndef ICP_TIMER_H
2 #define ICP_TIMER_H
3 
4 class ICPTimer
5 {
6 private:
7  std::string m_title;
8  struct timeb m_tb;
9  SYSTEMTIME m_st;
10  Poco::Timestamp m_ts;
11  struct timings_t
12  {
13  FILETIME create;
14  FILETIME exit;
15  FILETIME kernel;
16  FILETIME user;
17  };
20 
21  double diffFileTimesInMilliSec(const FILETIME& start, const FILETIME& finish);
22  LONGLONG diffFileTimes(const FILETIME& start, const FILETIME& finish);
23 
24 public:
25  ICPTimer(const char* title = NULL);
26  ~ICPTimer();
27  void start(const char* title = NULL);
28  double info(const char* title, std::ostream& os, bool add_nl = true);
29 };
30 
32 double ICPTimer::diffFileTimesInMilliSec(const FILETIME& start, const FILETIME& finish)
33 {
34  return diffFileTimes(start, finish) / 1.0e4;
35 }
36 
38 LONGLONG ICPTimer::diffFileTimes(const FILETIME& start, const FILETIME& finish)
39 {
40  ULARGE_INTEGER ularge1, ularge2;
41  ularge1.LowPart = start.dwLowDateTime;
42  ularge1.HighPart = start.dwHighDateTime;
43  ularge2.LowPart = finish.dwLowDateTime;
44  ularge2.HighPart= finish.dwHighDateTime;
45  if (ularge2.QuadPart > ularge1.QuadPart)
46  {
47  return ularge2.QuadPart - ularge1.QuadPart;
48  }
49  else
50  {
51  return -static_cast<LONGLONG>(ularge1.QuadPart - ularge2.QuadPart);
52  }
53 }
54 
55 ICPTimer::ICPTimer(const char* title)
56 {
57  start(title);
58 }
59 
61 {
62 }
63 
64 void ICPTimer::start(const char* title)
65 {
66  if (title != NULL)
67  {
68  m_title = title;
69  }
70  m_ts.fromEpochTime(time(NULL));
71  GetSystemTime(&m_st);
72  ftime(&m_tb);
73  if (GetThreadTimes(GetCurrentThread(), &m_thread_times.create, &m_thread_times.exit, &m_thread_times.kernel, &m_thread_times.user) == 0)
74  {
75  memset(&m_thread_times, 0, sizeof(m_thread_times));
76  }
77  if (GetProcessTimes(GetCurrentProcess(), &m_process_times.create, &m_process_times.exit, &m_process_times.kernel, &m_process_times.user) == 0)
78  {
79  memset(&m_process_times, 0, sizeof(m_process_times));
80  }
81 }
82 
83 double ICPTimer::info(const char* title, std::ostream& os, bool add_nl)
84 {
85  ICPTimer tim_end;
86 // double tdiff1ms = ( (tim_end.m_tb.millitm - m_tb.millitm) +
87 // 1000.0 * difftime(tim_end.m_tb.time, m_tb.time) );
88 // double tdiff1 = tdiff1ms / 1000.0;
89 // tdiff2 does not allow for a day change
90 // double tdiff2 = 3600.0 * (double)(tim_end.m_st.wHour - m_st.wHour) +
91 // 60.0 * (double)(tim_end.m_st.wMinute - m_st.wMinute) +
92 // (double)(tim_end.m_st.wSecond - m_st.wSecond) +
93 // (double)(tim_end.m_st.wMilliseconds - m_st.wMilliseconds) / 1000.0;
94  double tdiff3 = m_ts.elapsed() / 1.0e6;
95  os << "TIMING (" << m_title << "," << title << "): elasped(" << tdiff3 <<
96  ") thread(kernel=" << diffFileTimesInMilliSec(m_thread_times.kernel, tim_end.m_thread_times.kernel) / 1.0e3 <<
97  ", user=" << diffFileTimesInMilliSec(m_thread_times.user, tim_end.m_thread_times.user) / 1.0e3 <<
98  ") process(kernel=" << diffFileTimesInMilliSec(m_process_times.kernel, tim_end.m_process_times.kernel) / 1.0e3 <<
99  ", user=" << diffFileTimesInMilliSec(m_process_times.user, tim_end.m_process_times.user) / 1.0e3 << (add_nl ? ")\n" : ")");
100  return tdiff3;
101 }
102 
103 #endif /* ICP_TIMER_H */
Poco::Timestamp m_ts
Definition: icptimer.h:10
SYSTEMTIME m_st
Definition: icptimer.h:9
LONGLONG diffFileTimes(const FILETIME &start, const FILETIME &finish)
finish - start, returned in 100ns units
Definition: icptimer.h:38
timings_t m_thread_times
Definition: icptimer.h:18
double diffFileTimesInMilliSec(const FILETIME &start, const FILETIME &finish)
finish - start in milliseconds
Definition: icptimer.h:32
std::string m_title
Definition: icptimer.h:7
ICPTimer()
Definition: icputils.cpp:29
double info(const char *title, std::ostream &os, bool add_nl=true)
Definition: icptimer.h:83
void start(const char *title=NULL)
Definition: icptimer.h:64
FILETIME kernel
Definition: icptimer.h:15
FILETIME create
Definition: icptimer.h:13
timings_t m_process_times
Definition: icptimer.h:19
~ICPTimer()
Definition: icptimer.h:60
struct timeb m_tb
Definition: icptimer.h:8