Timer Class for both Unix/Linux/Mac and Windows system

Timer.h

 1 //////////////////////////////////////////////////////////////////////////////
2 // 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
13 //////////////////////////////////////////////////////////////////////////////
14
15 #ifndef TIMER_H_DEF
16 #define TIMER_H_DEF
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
25 class Timer
26 {
27 public:
28 Timer(); // default constructor
29 ~Timer(); // default destructor
30
31 void start(); // start timer
32 void stop(); // stop the timer
33 double getElapsedTime(); // get elapsed time in second
34 double getElapsedTimeInSec(); // get elapsed time in second (same as getElapsedTime)
35 double getElapsedTimeInMilliSec(); // get elapsed time in milli-second
36 double getElapsedTimeInMicroSec(); // get elapsed time in micro-second
37
38
39 protected:
40
41
42 private:
43 double startTimeInMicroSec; // starting time in micro-second
44 double endTimeInMicroSec; // ending time in micro-second
45 int stopped; // stop flag
46 #ifdef WIN32
47 LARGE_INTEGER frequency; // ticks per second
48 LARGE_INTEGER startCount; //
49 LARGE_INTEGER endCount; //
50 #else
51 timeval startCount; //
52 timeval endCount; //
53 #endif
54 };
55
56 #endif // TIMER_H_DEF

Timer.cpp

  1 //////////////////////////////////////////////////////////////////////////////
2 // 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
13 //////////////////////////////////////////////////////////////////////////////
14
15 #include "Timer.h"
16 #include <stdlib.h>
17
18 ///////////////////////////////////////////////////////////////////////////////
19 // constructor
20 ///////////////////////////////////////////////////////////////////////////////
21 Timer::Timer()
22 {
23 #ifdef WIN32
24 QueryPerformanceFrequency(&frequency);
25 startCount.QuadPart = 0;
26 endCount.QuadPart = 0;
27 #else
28 startCount.tv_sec = startCount.tv_usec = 0;
29 endCount.tv_sec = endCount.tv_usec = 0;
30 #endif
31
32 stopped = 0;
33 startTimeInMicroSec = 0;
34 endTimeInMicroSec = 0;
35 }
36
37
38
39 ///////////////////////////////////////////////////////////////////////////////
40 // distructor
41 ///////////////////////////////////////////////////////////////////////////////
42 Timer::~Timer()
43 {
44 }
45
46
47
48 ///////////////////////////////////////////////////////////////////////////////
49 // start timer.
50 // startCount will be set at this point.
51 ///////////////////////////////////////////////////////////////////////////////
52 void Timer::start()
53 {
54 stopped = 0; // reset stop flag
55 #ifdef WIN32
56 QueryPerformanceCounter(&startCount);
57 #else
58 gettimeofday(&startCount, NULL);
59 #endif
60 }
61
62
63
64 ///////////////////////////////////////////////////////////////////////////////
65 // stop the timer.
66 // endCount will be set at this point.
67 ///////////////////////////////////////////////////////////////////////////////
68 void Timer::stop()
69 {
70 stopped = 1; // set timer stopped flag
71
72 #ifdef WIN32
73 QueryPerformanceCounter(&endCount);
74 #else
75 gettimeofday(&endCount, NULL);
76 #endif
77 }
78
79
80
81 ///////////////////////////////////////////////////////////////////////////////
82 // compute elapsed time in micro-second resolution.
83 // other getElapsedTime will call this first, then convert to correspond resolution.
84 ///////////////////////////////////////////////////////////////////////////////
85 double Timer::getElapsedTimeInMicroSec()
86 {
87 #ifdef WIN32
88 if(!stopped)
89 QueryPerformanceCounter(&endCount);
90
91 startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
92 endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
93 #else
94 if(!stopped)
95 gettimeofday(&endCount, NULL);
96
97 startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
98 endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
99 #endif
100
101 return endTimeInMicroSec - startTimeInMicroSec;
102 }
103
104
105
106 ///////////////////////////////////////////////////////////////////////////////
107 // divide elapsedTimeInMicroSec by 1000
108 ///////////////////////////////////////////////////////////////////////////////
109 double Timer::getElapsedTimeInMilliSec()
110 {
111 return this->getElapsedTimeInMicroSec() * 0.001;
112 }
113
114
115
116 ///////////////////////////////////////////////////////////////////////////////
117 // divide elapsedTimeInMicroSec by 1000000
118 ///////////////////////////////////////////////////////////////////////////////
119 double Timer::getElapsedTimeInSec()
120 {
121 return this->getElapsedTimeInMicroSec() * 0.000001;
122 }
123
124
125
126 ///////////////////////////////////////////////////////////////////////////////
127 // same as getElapsedTimeInSec()
128 ///////////////////////////////////////////////////////////////////////////////
129 double Timer::getElapsedTime()
130 {
131 return this->getElapsedTimeInSec();
132 }

How to use

 1 #include <iostream>
2 #include "Timer.h"
3 using namespace std;
4
5 int main()
6 {
7 Timer timer;
8
9 // start timer
10 timer.start();
11
12 // do something
13 ...
14
15 // stop timer
16 timer.stop();
17
18 // print the elapsed time in millisec
19 cout << timer.getElapsedTimeInMilliSec() << " ms.\n";
20
21 return 0;
22 }

 

trackback: http://www.songho.ca/misc/timer/timer.html


 

posted @ 2011-10-14 03:54  大有|元亨  阅读(397)  评论(0编辑  收藏  举报