-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.cpp
More file actions
85 lines (63 loc) · 1.64 KB
/
timer.cpp
File metadata and controls
85 lines (63 loc) · 1.64 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
#include "timer.h"
//---------------------------------------------------------------------
Timer::Timer()
{
elapsed_ = 0;
}
//---------------------------------------------------------------------
Timer::Timer(double swTime): swTime_(swTime)
{
elapsed_ = 0;
}
//---------------------------------------------------------------------
void Timer::setTimer(double swTime)
{
swTime_ = swTime;
}
//---------------------------------------------------------------------
double Timer::now()
{
timeval t;
gettimeofday(&t, NULL);
return toSeconds(t);
}
//---------------------------------------------------------------------
void Timer::start()
{
timeval t;
gettimeofday(&t, NULL);
start_ = toSeconds(t);
}
//---------------------------------------------------------------------
void Timer::stop()
{
timeval t;
gettimeofday(&t, NULL);
elapsed_ = toSeconds(t) - start_;
}
//---------------------------------------------------------------------
double Timer::elapsed()
{
#ifndef NDEBUG
timeval t;
gettimeofday(&t, NULL);
assert( toSeconds(t) - start_ >= elapsed_);
#endif
return elapsed_;
}
//---------------------------------------------------------------------
bool Timer::timeUp()
{
timeval t;
gettimeofday(&t, NULL);
if (toSeconds(t) - start_ >= swTime_)
return true;
return false;
}
//---------------------------------------------------------------------
double Timer::toSeconds(timeval t)
{
return t.tv_sec + t.tv_usec / double(MICRO_IN_SEC);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------