code_profiler
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 (song.ahn@gmail.com)
9 // CREATED: 2003-01-13
10 // UPDATED: 2006-01-13
11 //
12 // Copyright (c) 2003 Song Ho Ahn
14 
15 #include "tue/profiling/timer.h"
16 #include <stdlib.h>
17 
18 namespace tue
19 {
20 
21 #ifdef WIN32
22 inline long double timeCountsToLongDouble(const LARGE_INTEGER& counts, const LARGE_INTEGER& frequency)
23 {
24  return counts.QuadPart * (1000000.0 / frequency.QuadPart);
25 }
26 #else
27 inline long double timevalToLongDouble(const timeval& time)
28 {
29  return (time.tv_sec * 1000000.0) + time.tv_usec;
30 }
31 #endif
32 
33 // ----------------------------------------------------------------------------------------------------
34 
35 Timer::Timer() : running_(false)
36 {
37 #ifdef WIN32
38  QueryPerformanceFrequency(&frequency_);
39  startCount_.QuadPart = 0;
40  endCount_.QuadPart = 0;
41 #else
42  start_count_.tv_sec = start_count_.tv_usec = 0;
43  end_count_.tv_sec = end_count_.tv_usec = 0;
44 #endif
45 }
46 
48 {
49 }
50 
52 {
53  running_ = true;
54 #ifdef WIN32
55  QueryPerformanceCounter(&startCount_);
56 #else
57  gettimeofday(&start_count_, NULL);
58 #endif
59 }
60 
62 {
63  running_ = false;
64 
65 #ifdef WIN32
66  QueryPerformanceCounter(&endCount_);
67 #else
68  gettimeofday(&end_count_, NULL);
69 #endif
70 }
71 
73 {
74 #ifdef WIN32
75  LARGE_INTEGER endCount;
76  if(!running_)
77  endCount = endCount_;
78  else
79  QueryPerformanceCounter(&endCount);
80 
81  long double startTimeInMicroSec = timeCountsToLongDouble(startCount_, frequency_);
82  long double endTimeInMicroSec = timeCountsToLongDouble(endCount, frequency_);
83 #else
84  timeval end_count;
85  if (!running_)
86  end_count = end_count_;
87  else
88  gettimeofday(&end_count, NULL);
89 
90  long double startTimeInMicroSec = timevalToLongDouble(start_count_);
91  long double endTimeInMicroSec = timevalToLongDouble(end_count);
92 #endif
93 
94  return endTimeInMicroSec - startTimeInMicroSec;
95 }
96 
98 {
99  return this->getElapsedTimeInMicroSec() * 0.001;
100 }
101 
102 long double Timer::getElapsedTimeInSec() const
103 {
104  return this->getElapsedTimeInMicroSec() * 0.000001;
105 }
106 
107 long double Timer::getElapsedTime() const
108 {
109  return this->getElapsedTimeInSec();
110 }
111 
113 {
114  std::cout << m << " (sec): " << getElapsedTimeInSec() << std::endl;
115 }
116 
117 
119 {
120  std::cout << m << " (msec): " << getElapsedTimeInMilliSec() << std::endl;
121 }
122 
123 long double Timer::nowMicroSec()
124 {
125 #ifdef WIN32
126  LARGE_INTEGER nowCount;
127  QueryPerformanceCounter(&nowCount);
128  return timeCountsToLongDouble(nowCount);
129 #else
130  timeval now_count;
131  gettimeofday(&now_count, NULL);
132  return timevalToLongDouble(now_count);
133 #endif
134 }
135 
136 long double Timer::nowMilliSec()
137 {
138  return Timer::nowMicroSec() * 0.001;
139 }
140 
141 long double Timer::now()
142 {
143 return Timer::nowMicroSec() * 0.000001;
144 }
145 
146 }
147 
tue::Timer::getElapsedTime
long double getElapsedTime() const
Alias of Timer::getElapsedTimeInSec.
Definition: timer.cpp:107
std::string
tue::Timer::getElapsedTimeInSec
long double getElapsedTimeInSec() const
Get elasped time in seconds.
Definition: timer.cpp:102
tue::Timer::printLastElapsedTimeMSec
void printLastElapsedTimeMSec(std::string)
Definition: timer.cpp:118
tue::timevalToLongDouble
long double timevalToLongDouble(const timeval &time)
Definition: timer.cpp:27
tue::Timer::Timer
Timer()
Definition: timer.cpp:35
tue::Timer::start_count_
timeval start_count_
Start counter.
Definition: timer.h:116
tue::Timer::nowMicroSec
static long double nowMicroSec()
Get the current time in micro-seconds since epoch.
Definition: timer.cpp:123
tue::Timer::nowMilliSec
static long double nowMilliSec()
Get the current time in milli-seconds since epoch.
Definition: timer.cpp:136
tue::Timer::getElapsedTimeInMicroSec
long double getElapsedTimeInMicroSec() const
Get elasped time in micro-seconds.
Definition: timer.cpp:72
tue::Timer::end_count_
timeval end_count_
End counter.
Definition: timer.h:121
tue::Timer::stop
void stop()
Definition: timer.cpp:61
tue::Timer::~Timer
~Timer()
Definition: timer.cpp:47
tue::Timer::start
void start()
Definition: timer.cpp:51
std::cout
tue::Timer::getElapsedTimeInMilliSec
long double getElapsedTimeInMilliSec() const
Get elasped time in milli-seconds.
Definition: timer.cpp:97
timer.h
tue::Timer::now
static long double now()
Get the current time in seconds since epoch.
Definition: timer.cpp:141
tue::Timer::running_
bool running_
Running flag.
Definition: timer.h:95
std::endl
T endl(T... args)
tue::Timer::printLastElapsedTime
void printLastElapsedTime(std::string)
Definition: timer.cpp:112
tue
Definition: loop_timer.h:8