Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.cpp
Go to the documentation of this file.
1 // Timer.cpp
3 // =========
4 // High Resolution Timer.
5 // This timer is able to measure the elapsed time with 1 micro-second accuracy
6 // in both Windows, Linux and Unix system
7 //
8 // AUTHOR: Song Ho Ahn ([email protected])
9 // CREATED: 2003-01-13
10 // UPDATED: 2006-01-13
11 //
12 // Copyright (c) 2003 Song Ho Ahn
14 
15 #include "util/Timer.hpp"
16 #include <cstdlib>
17 
19 // constructor
21 Timer::Timer(bool bStart)
22 {
23  clear();
24 
25  // FT: Added this part:
26  //
27  if (bStart) start();
28 }
29 
31 // distructor
34 {
35 }
36 
38 // start timer.
39 // startCount will be set at this point.
42 {
43  stopped = 0; // reset stop flag
44 #ifdef WIN32
45  QueryPerformanceCounter(&startCount);
46 #else
47  gettimeofday(&startCount, nullptr);
48 #endif
49 }
50 
52 // stop the timer.
53 // endCount will be set at this point.
56 {
57  stopped = 1; // set timer stopped flag
58 
59 #ifdef WIN32
60  QueryPerformanceCounter(&endCount);
61 #else
62  gettimeofday(&endCount, nullptr);
63 #endif
64 }
65 
67 // Clear: stop the timer and clear the contents.
70 {
71 #ifdef WIN32
72  QueryPerformanceFrequency(&frequency);
73  startCount.QuadPart = 0;
74  endCount.QuadPart = 0;
75 #else
76  startCount.tv_sec = startCount.tv_usec = 0;
77  endCount.tv_sec = endCount.tv_usec = 0;
78 #endif
79 
80  stopped = 0;
81  startTimeInMicroSec = 0;
82  endTimeInMicroSec = 0;
83 }
84 
86 // compute elapsed time in micro-second resolution.
87 // other getElapsedTime will call this first, then convert to correspond
88 // resolution.
91 {
92 #ifdef WIN32
93  if (!stopped) QueryPerformanceCounter(&endCount);
94 
95  startTimeInMicroSec =
96  startCount.QuadPart * (1000000.0 / frequency.QuadPart);
97  endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
98 #else
99  if (!stopped) gettimeofday(&endCount, nullptr);
100 
101  startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
102  endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
103 #endif
104 
105  return endTimeInMicroSec - startTimeInMicroSec;
106 }
107 
109 // divide elapsedTimeInMicroSec by 1000
112 {
113  return getElapsedTimeInMicroSec() * 0.001;
114 }
115 
117 // divide elapsedTimeInMicroSec by 1000000
120 {
121  return getElapsedTimeInMicroSec() * 0.000001;
122 }
123 
125 // same as getElapsedTimeInSec()
128 {
129  return getElapsedTimeInSec();
130 }
EXPORT double getElapsedTimeInMilliSec()
Definition: Timer.cpp:111
void stop()
Definition: Timer.cpp:55
EXPORT void start()
Definition: Timer.cpp:41
double getElapsedTime()
Definition: Timer.cpp:127
void clear()
Definition: Timer.cpp:69
double getElapsedTimeInMicroSec()
Definition: Timer.cpp:90
EXPORT Timer(bool bStart=false)
Definition: Timer.cpp:21
double getElapsedTimeInSec()
Definition: Timer.cpp:119
EXPORT ~Timer()
Definition: Timer.cpp:33