-
Notifications
You must be signed in to change notification settings - Fork 7
/
timer.cpp
174 lines (129 loc) · 4.17 KB
/
timer.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
/* **************************************************************************
timer.cpp timer for C/C++ programs
20-Jun-2006 PMC gettimeofday() not available on MingW,
changed this to pure elapsed time only,
to accomodate MingW and timing problems
on Windoze
Copyright 2003-2006 P.M.Cronje
This file is part of the Double Dummer Driver (DDD).
DDD is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DDD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DDD; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "portab_DDD.h"
#include "timer.h"
// *****************************************************************************
// timer workaround for Windoze,
// must define _WIN32 for compilation
// *****************************************************************************
#if defined(_WIN32)
struct sFileTime
{
unsigned int dwLowDateTime;
unsigned int dwHighDateTime;
};
#ifdef _MSC_VER
struct timeval
{
long tv_sec, tv_usec;
};
#endif
extern "C"
{
void _stdcall GetSystemTimeAsFileTime(struct sFileTime *pft);
}
void gettimeofday(struct timeval* p, void* pv);
void gettimeofday(struct timeval* p, void* pv)
{
union
{
long long ns100; // time since 1 Jan 1601 in 100ns units
struct sFileTime ft;
} now;
GetSystemTimeAsFileTime(&(now.ft));
p->tv_usec = static_cast<int>((now.ns100 / 10LL) % 1000000LL);
p->tv_sec = static_cast<int>((now.ns100 - (116444736000000000LL))/10000000LL);
UNUSED(pv);
}
#endif
// *****************************************************************************
// cTimer
// *****************************************************************************
cTimer::cTimer()
{
bStarted = false;
dElapsed = prevdElapsed = deltaElapsed = 0.0;
} // cTimer::cTimer
// *****************************************************************************
cTimer::~cTimer()
{
stop();
} // cTimer::~cTimer
// *****************************************************************************
void cTimer::check()
{
// find elapsed statistics
if(bStarted)
{
getTimerInfo(&dElapsed);
deltaElapsed = dElapsed - prevdElapsed;
prevdElapsed = dElapsed;
dElapsed -= dElapsed0;
}
else
start();
} // cTimer::check
// *****************************************************************************
void cTimer::getFormattedTime(char sztime[32])
{
// DD-MON-YYYY HH:MM:SS
time_t timeval;
struct tm *ptm;
// find the current date and time
time(&timeval);
ptm = localtime(&timeval);
strftime(sztime,21,"%d-%b-%Y %H:%M:%S",ptm);
} // cTimer::getFormattedTime
// *****************************************************************************
void cTimer::getTimerInfo(double *pdelapsed)
{
struct timeval tv;
// elapsed
gettimeofday(&tv,0);
*pdelapsed = static_cast<double>(tv.tv_sec) +
0.000001 * static_cast<double>(tv.tv_usec);
if(*pdelapsed < 0.000001)
*pdelapsed = 0.000001;
} // cTimer::getTimerInfo
// *****************************************************************************
void cTimer::start()
{
getTimerInfo(&dElapsed0);
dElapsed = dElapsed0;
prevdElapsed = dElapsed;
deltaElapsed = 0.0;
bStarted = true;
} // cTimer::start
// *****************************************************************************
void cTimer::stop()
{
if(bStarted)
{
check();
bStarted = false;
}
} // cTimer::stop
// *****************************************************************************