code_profiler
timer.h
Go to the documentation of this file.
1 // Timer.h
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 #ifndef TUE_PROFILING_TIMER_H_
16 #define TUE_PROFILING_TIMER_H_
17 
18 #ifdef WIN32 // Windows system specific
19 #include <windows.h>
20 #else // Unix based system specific
21 #include <sys/time.h>
22 #endif
23 
24 #include <iostream>
25 #include <string>
26 
27 namespace tue
28 {
29 
30 class Timer
31 {
32 public:
33  Timer();
34  ~Timer();
35 
36  void start();
37  void stop();
38 
43  inline bool running() const { return running_; }
44 
49  long double getElapsedTime() const;
50 
55  long double getElapsedTimeInSec() const;
56 
61  long double getElapsedTimeInMilliSec() const;
62 
67  long double getElapsedTimeInMicroSec() const;
68 
71 
76  static long double nowMicroSec();
77 
82  static long double nowMilliSec();
83 
88  static long double now();
89 
90 
91 private:
95  bool running_;
96 
97 #ifdef WIN32
98 
101  LARGE_INTEGER frequency_;
102 
106  LARGE_INTEGER startCount_;
107 
111  LARGE_INTEGER endCount_;
112 #else
113 
116  timeval start_count_;
117 
121  timeval end_count_;
122 #endif
123 };
124 
125 }
126 
127 #define TIMER_START Timer t; t.start();
128 #define TIMER_STOP(x) (t. printLastElapsedTime(x))
129 #define TIMER_STOP_M(x) (t.printLastElapsedTimeMSec(x))
130 
131 #endif // TUE_PROFILING_TIMER_H_
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::running
bool running() const
Get running status of the timer.
Definition: timer.h:43
tue::Timer::printLastElapsedTimeMSec
void printLastElapsedTimeMSec(std::string)
Definition: timer.cpp:118
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
iostream
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
tue::Timer::getElapsedTimeInMilliSec
long double getElapsedTimeInMilliSec() const
Get elasped time in milli-seconds.
Definition: timer.cpp:97
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
tue::Timer
Definition: timer.h:30
tue::Timer::printLastElapsedTime
void printLastElapsedTime(std::string)
Definition: timer.cpp:112
tue
Definition: loop_timer.h:8
string