-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimer.cpp
More file actions
executable file
·104 lines (92 loc) · 3 KB
/
Timer.cpp
File metadata and controls
executable file
·104 lines (92 loc) · 3 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
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <sys/time.h>
using namespace std;
#include "Timer.hpp"
void Error(const char* msg) {
printf("%s\n",msg);
abort();
}
/*********************************************************************/
//constructors and destructors
/*********************************************************************/
Timer::Timer(const Timer &rhs) { *this = rhs; }
/*********************************************************************/
//operators
/*********************************************************************/
const Timer &Timer::operator = (const Timer &rhs)
{
start = rhs.start;
time = rhs.time;
return *this;
}
/*********************************************************************/
//return true if the timer is running and false otherwise
/*********************************************************************/
bool Timer::Running() const { return (start != -1); }
/*********************************************************************/
//read the current time. the timer must be stopped.
/*********************************************************************/
double Timer::Read() const
{
if(start != -1) {
Error("ERROR: trying to read a running timer ...\n");
}
return time;
}
/*********************************************************************/
//forward a timer by the supplied amount of time
/*********************************************************************/
void Timer::Forward(const Timer &arg)
{
if(arg.start != -1) {
Error("ERROR: trying to forward by a running timer ...\n");
}
if(start != -1) {
Error("ERROR: trying to forward a running timer ...\n");
}
time += arg.time;
}
/*********************************************************************/
//start the timer
/*********************************************************************/
void Timer::Start()
{
if(start != -1) {
Error("ERROR: trying to start a running timer ...\n");
}
#ifdef WIN32
start = static_cast<double>(::time(NULL));
#else
struct timeval tv;
if(gettimeofday(&tv,NULL) == -1) {
Error("ERROR: could not get current time for starting timer ...\n");
}
start = tv.tv_sec + (tv.tv_usec / 1000000.0);
#endif //WIN32
}
/*********************************************************************/
//stop the timer
/*********************************************************************/
void Timer::Stop()
{
if(start == -1) {
Error("WARNING: Timer already stopped\n");
return;
}
#ifdef WIN32
double currTime = static_cast<double>(::time(NULL));
#else
struct timeval tv;
if(gettimeofday(&tv,NULL) == -1) {
Error("ERROR: could not get current time for stopping timer ...\n");
}
double currTime = tv.tv_sec + (tv.tv_usec / 1000000.0);
#endif //WIN32
time += (currTime - start);
start = -1;
}
/*********************************************************************/
//end of Timer.cpp
/*********************************************************************/