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 "profiling/Timer.h"
16 #include <stdlib.h>
17 
19 // constructor
22 {
23 #ifdef WIN32
24  QueryPerformanceFrequency(&frequency);
25  startCount.QuadPart = 0;
26  endCount.QuadPart = 0;
27 #else
28  start_count_.tv_sec = start_count_.tv_usec = 0;
29  end_count_.tv_sec = end_count_.tv_usec = 0;
30 #endif
31 
32  stopped = 0;
33 }
34 
35 
36 
38 // distructor
41 {
42 }
43 
44 
45 
47 // start timer.
48 // startCount will be set at this point.
51 {
52  stopped = 0; // reset stop flag
53 #ifdef WIN32
54  QueryPerformanceCounter(&startCount);
55 #else
56  gettimeofday(&start_count_, NULL);
57 #endif
58 }
59 
60 
61 
63 // stop the timer.
64 // endCount will be set at this point.
67 {
68  stopped = 1; // set timer stopped flag
69 
70 #ifdef WIN32
71  QueryPerformanceCounter(&endCount);
72 #else
73  gettimeofday(&end_count_, NULL);
74 #endif
75 }
76 
77 
78 
80 // compute elapsed time in micro-second resolution.
81 // other getElapsedTime will call this first, then convert to correspond resolution.
84 {
85 #ifdef WIN32
86  if(!stopped)
87  QueryPerformanceCounter(&endCount);
88 
89  double startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
90  double endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
91 #else
92  timeval end_count;
93  if (stopped) {
94  end_count = end_count_;
95  } else {
96  gettimeofday(&end_count, NULL);
97  }
98 
99  double startTimeInMicroSec = (start_count_.tv_sec * 1000000.0) + start_count_.tv_usec;
100  double endTimeInMicroSec = (end_count.tv_sec * 1000000.0) + end_count.tv_usec;
101 #endif
102 
103  return endTimeInMicroSec - startTimeInMicroSec;
104 }
105 
106 
107 
109 // divide elapsedTimeInMicroSec by 1000
112 {
113  return this->getElapsedTimeInMicroSec() * 0.001;
114 }
115 
116 
117 
119 // divide elapsedTimeInMicroSec by 1000000
122 {
123  return this->getElapsedTimeInMicroSec() * 0.000001;
124 }
125 
126 
127 
129 // same as getElapsedTimeInSec()
131 double Timer::getElapsedTime() const
132 {
133  return this->getElapsedTimeInSec();
134 }
135 
137 {
138  std::cout << m << " (sec): " << getElapsedTimeInSec() << std::endl;
139 }
140 
141 
143 {
144  std::cout << m << " (msec): " << getElapsedTimeInMilliSec() << std::endl;
145 }
146 
Timer::start_count_
timeval start_count_
Definition: Timer.h:56
std::string
Timer::~Timer
~Timer()
Definition: Timer.cpp:40
Timer::getElapsedTimeInSec
double getElapsedTimeInSec() const
Definition: Timer.cpp:121
Timer::getElapsedTime
double getElapsedTime() const
Definition: Timer.cpp:131
std::cout
Timer::stopped
int stopped
Definition: Timer.h:50
Timer::getElapsedTimeInMicroSec
double getElapsedTimeInMicroSec() const
Definition: Timer.cpp:83
Timer::start
void start()
Definition: Timer.cpp:50
Timer.h
Timer::end_count_
timeval end_count_
Definition: Timer.h:57
Timer::getElapsedTimeInMilliSec
double getElapsedTimeInMilliSec() const
Definition: Timer.cpp:111
Timer::Timer
Timer()
Definition: Timer.cpp:21
std::endl
T endl(T... args)
Timer::printLastElapsedTime
void printLastElapsedTime(std::string)
Definition: Timer.cpp:136
Timer::printLastElapsedTimeMSec
void printLastElapsedTimeMSec(std::string)
Definition: Timer.cpp:142
Timer::stop
void stop()
Definition: Timer.cpp:66