-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
161 lines (143 loc) · 5.77 KB
/
timer.h
File metadata and controls
161 lines (143 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Timer.h documents a simple, platform-independent interval timer
// that measures CPU usage in (fractional) seconds.
//
// Author: Joel Adams, for Hands On C++.
// Date: November 1997
//
// Revision History
//
// Seconds() added - kvlinden, 5-Feb-98
//
// ANSI compliance, interface improvements
// - Adams, March, 1998.
//
#ifndef TIMER
#define TIMER
#include <iostream> // ostream
#include <ctime> // C time library: clock_t, clock(), CLOCKS_PER_SEC
using namespace std;
class Timer
{
public:
Timer();
void Start();
void Stop();
void Reset();
double Clocks() const;
double Seconds() const;
void Print(ostream & out) const;
private:
clock_t myStartTime;
clock_t myRunTime;
bool running;
};
//***************************************************************
// Timer constructor. *
// Postcondition: myStartTime == 0 && myRunTime == 0 && *
// running == false. *
//***************************************************************
inline Timer::Timer()
{
myStartTime = myRunTime = 0;
running = false;
}
//***************************************************************
// Start myself. *
// Postcondition: myStartTime == the current clock value && *
// running == true. *
// Note: Start() while I'm running re-starts me. *
//***************************************************************
inline void Timer::Start()
{
running = true;
myStartTime = clock();
}
//***************************************************************
// Stop myself. *
// Precondition: running == true. *
// Postcondition: myRunTime has been updated with time interval *
// since the Timer was started && *
// running == false. *
// Note: Stop() only has an effect when I'm running. *
//***************************************************************
inline void Timer::Stop()
{
if (running)
{
myRunTime += clock() - myStartTime;
running = false;
}
}
//***************************************************************
// Reset myself. *
// Postcondition: myStartTime is the current clock value && *
// myRunTime == 0. *
// Note: Reset() while I'm running re-starts me. *
//***************************************************************
inline void Timer::Reset()
{
myRunTime = 0;
myStartTime = clock();
}
//***************************************************************
// My current time value (in clocks) *
// Return: the elapsed time since I was started. *
// Note: If I'm not running: *
// repeated calls to Clocks() return the same value. *
// Otherwise: *
// repeated calls to Clocks() return different values. *
//***************************************************************
inline double Timer::Clocks() const
{
if (running)
return double(clock() - myStartTime);
else
return double(myRunTime);
}
//***************************************************************
// My current time value (in seconds) *
// Return: the elapsed time since I was started. *
// Note: If I'm not running: *
// repeated calls to Seconds() return the same value. *
// Otherwise: *
// repeated calls to Seconds() return different values. *
//***************************************************************
inline double Timer::Seconds() const
{
if (running)
return double(clock() - myStartTime) / double(CLOCKS_PER_SEC);
else
return double(myRunTime) / double(CLOCKS_PER_SEC);
}
//***************************************************************
// Output myself (function member). *
// Receive: out, an ostream. *
// Output: the time since I was started. *
// Passback: out, containing the Timer output. *
// Note: If I'm not running: *
// repeated calls to Print() display the same value. *
// Otherwise: *
// repeated calls to Print() display different values. *
//***************************************************************
inline void Timer::Print(ostream & out) const
{
out << Seconds();
}
//***************************************************************
// Output a Timer (operator<<) *
// Receive: out, an ostream, *
// aTimer, a Timer. *
// Output: the value of aTimer via out. *
// Passback: out, containing the time since aTimer was started. *
// Return: out, for output chaining. *
// Note: If I'm not running: *
// repeated calls to << display the same value. *
// Otherwise: *
// repeated calls to << display different values. *
//***************************************************************
inline ostream & operator<< (ostream & out, const Timer & aTimer)
{
aTimer.Print(out);
return out;
}
#endif