-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stopwatch.C
63 lines (54 loc) · 1.46 KB
/
Stopwatch.C
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
#include "Stopwatch.h"
// Initializes the stopwatch object
Stopwatch::Stopwatch()
{
_isStarted = 0;
_startTime.tv_sec = 0;
_startTime.tv_usec = 0;
}
void Stopwatch::start()
{
gettimeofday(&_startTime, 0);
_isStarted = 1;
}
timeval Stopwatch::timeElapsed()
{
timeval zeroTime;
timeval currentTime;
timeval elapsedTime;
zeroTime.tv_sec = 0;
zeroTime.tv_usec = 0;
gettimeofday(¤tTime, 0);
if (!_isStarted || timeval_subtract(elapsedTime, currentTime, _startTime)) {
return zeroTime;
}
else {
return elapsedTime;
}
}
float Stopwatch::secondsElapsed()
{
timeval elapsed = timeElapsed();
return elapsed.tv_sec + elapsed.tv_usec / (float)US_PER_SEC;
}
// Helper function to find differences in time, accurate to the
// microsecond. Return 1 if result is negative
int Stopwatch::timeval_subtract (timeval &result, timeval t1, timeval t0)
{
// perform the carry for the later subtraction by updating y
if (t1.tv_usec < t0.tv_usec) {
int nsec = (t0.tv_usec - t1.tv_usec) / 1000000 + 1;
t0.tv_usec -= 1000000 * nsec;
t0.tv_sec += nsec;
}
if (t1.tv_usec - t0.tv_usec > 1000000) {
int nsec = (t1.tv_usec - t0.tv_usec) / 1000000;
t0.tv_usec += 1000000 * nsec;
t0.tv_sec -= nsec;
}
// Compute the time remaining to wait. tv_usec is certainly positive
result.tv_sec = t1.tv_sec - t0.tv_sec;
result.tv_usec = t1.tv_usec - t0.tv_usec;
// return 1 if result is negative
return t1.tv_sec < t0.tv_sec;
}