1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * timing.c -- Program timing routines
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: timing.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | /*
|
---|
25 | $Log$
|
---|
26 | Revision 1.1 2003/02/20 21:14:17 mdewsnip
|
---|
27 | Addition of MG package for search and retrieval
|
---|
28 |
|
---|
29 | Revision 1.1 1999/08/10 21:17:05 sjboddie
|
---|
30 | renamed mg-1.3d directory mg
|
---|
31 |
|
---|
32 | Revision 1.1 1998/11/17 09:32:49 rjmcnab
|
---|
33 | *** empty log message ***
|
---|
34 |
|
---|
35 | * Revision 1.1 1994/08/22 00:24:53 tes
|
---|
36 | * Initial placement under CVS.
|
---|
37 | *
|
---|
38 | */
|
---|
39 |
|
---|
40 | static char *RCSID = "$Id: timing.c 3745 2003-02-20 21:20:24Z mdewsnip $";
|
---|
41 |
|
---|
42 | #include "sysfuncs.h"
|
---|
43 | #include "timing.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Return the time in seconds since 00:00:00 GMT, Jan. 1, 1970 to the
|
---|
48 | * best precision possible
|
---|
49 | *
|
---|
50 | */
|
---|
51 | double
|
---|
52 | RealTime (void)
|
---|
53 | {
|
---|
54 | return ((double) time (NULL));
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Return the amount of system and user CPU used by the process to date to
|
---|
61 | * the best precision possible. If user is non-null then it is initialised to
|
---|
62 | * the user time. If sys is non-null then it is initialised to the system time.
|
---|
63 | *
|
---|
64 | */
|
---|
65 | double
|
---|
66 | CPUTime (double *user, double *sys)
|
---|
67 | {
|
---|
68 |
|
---|
69 | #if defined(HAVE_TIMES)
|
---|
70 |
|
---|
71 | struct tms buffer;
|
---|
72 | static double clk_tck = 0;
|
---|
73 | double u, s;
|
---|
74 |
|
---|
75 | times (&buffer);
|
---|
76 |
|
---|
77 | if (clk_tck == 0)
|
---|
78 | clk_tck = CLK_TCK;
|
---|
79 |
|
---|
80 | u = (double) buffer.tms_utime / clk_tck;
|
---|
81 | s = (double) buffer.tms_stime / clk_tck;
|
---|
82 | if (user)
|
---|
83 | *user = u;
|
---|
84 | if (sys)
|
---|
85 | *sys = s;
|
---|
86 | return u + s;
|
---|
87 |
|
---|
88 | #elif defined(HAVE_GETRUSAGE)
|
---|
89 |
|
---|
90 | struct rusage ruse;
|
---|
91 | getrusage (RUSAGE_SELF, &ruse);
|
---|
92 | if (user)
|
---|
93 | *user = (double) ruse.ru_utime.tv_sec +
|
---|
94 | (double) ruse.ru_utime.tv_usec / 1000000;
|
---|
95 | if (sys)
|
---|
96 | *sys = (double) ruse.ru_stime.tv_sec +
|
---|
97 | (double) ruse.ru_stime.tv_usec / 1000000;
|
---|
98 | return ((double) ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec +
|
---|
99 | ((double) ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) / 1000000);
|
---|
100 |
|
---|
101 | #else
|
---|
102 |
|
---|
103 | /* [RPAP - Feb 97: WIN32 Port] */
|
---|
104 | #ifdef __WIN32__
|
---|
105 | if (user) *user = 0.0;
|
---|
106 | if (sys) *sys = 0.0;
|
---|
107 | return 0.0;
|
---|
108 | #else
|
---|
109 | -->Can NOT find a system time routine to use ! !!
|
---|
110 | #endif /* __WIN32__ */
|
---|
111 |
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Get the Real and CPU time and store them in a ProgTime structure
|
---|
121 | */
|
---|
122 | void
|
---|
123 | GetTime (ProgTime * StartTime)
|
---|
124 | {
|
---|
125 | StartTime->RealTime = RealTime ();
|
---|
126 | StartTime->CPUTime = CPUTime (NULL, NULL);
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Display the Real and CPU time elapsed since the StartTime anf FinishTime
|
---|
134 | * structures were initialised. If FinishTime is NULL then FinishTime is
|
---|
135 | * Now.
|
---|
136 | */
|
---|
137 | char *
|
---|
138 | ElapsedTime (ProgTime * StartTime,
|
---|
139 | ProgTime * FinishTime)
|
---|
140 | {
|
---|
141 | static char buf[50];
|
---|
142 | double Real, CPU, hour, min, sec;
|
---|
143 | if (!FinishTime)
|
---|
144 | {
|
---|
145 | Real = RealTime () - StartTime->RealTime;
|
---|
146 | CPU = CPUTime (NULL, NULL) - StartTime->CPUTime;
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | Real = FinishTime->RealTime - StartTime->RealTime;
|
---|
151 | CPU = FinishTime->CPUTime - StartTime->CPUTime;
|
---|
152 | }
|
---|
153 | hour = floor (CPU / 3600);
|
---|
154 | min = floor ((CPU - hour * 3600) / 60);
|
---|
155 | sec = CPU - hour * 3600 - min * 60;
|
---|
156 | sprintf (buf, "%02.0f:%02.0f:%05.2f cpu, %02d:%02d:%02d elapsed.",
|
---|
157 | hour, min, sec,
|
---|
158 | ((int) ceil (Real)) / 3600,
|
---|
159 | (((int) ceil (Real)) % 3600) / 60, ((int) ceil (Real)) % 60);
|
---|
160 | return (buf);
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | #define MILLION 1000000
|
---|
165 |
|
---|
166 | /* ===============================================================================
|
---|
167 | * Function: cputime_string
|
---|
168 | * Description:
|
---|
169 | * Prints out given cpu time into a string buffer.
|
---|
170 | * Input: cpu-time in clock_t or timeval format
|
---|
171 | * Output: pointer to internal buffer
|
---|
172 | * =============================================================================== */
|
---|
173 |
|
---|
174 | #ifdef HAVE_TIMES
|
---|
175 | char *
|
---|
176 | cputime_string (clock_t clk)
|
---|
177 | {
|
---|
178 | static char buf[15];
|
---|
179 | double CPU, hour, min, sec;
|
---|
180 |
|
---|
181 | CPU = (double) clk / (double) CLK_TCK;
|
---|
182 | hour = floor (CPU / 3600);
|
---|
183 | min = floor ((CPU - hour * 3600) / 60);
|
---|
184 | sec = CPU - hour * 3600 - min * 60;
|
---|
185 | sprintf (buf, "%02.0f:%02.0f:%05.2f", hour, min, sec);
|
---|
186 | return (buf);
|
---|
187 | }
|
---|
188 | #else
|
---|
189 | char *
|
---|
190 | cputime_string (struct timeval *t)
|
---|
191 | {
|
---|
192 | static char buf[15];
|
---|
193 | double CPU, hour, min, sec;
|
---|
194 |
|
---|
195 | CPU = (double) t->tv_sec + (double) t->tv_usec / MILLION;
|
---|
196 | hour = floor (CPU / 3600);
|
---|
197 | min = floor ((CPU - hour * 3600) / 60);
|
---|
198 | sec = CPU - hour * 3600 - min * 60;
|
---|
199 | sprintf (buf, "%02.0f:%02.0f:%05.2f", hour, min, sec);
|
---|
200 | return (buf);
|
---|
201 | }
|
---|
202 | #endif
|
---|
203 |
|
---|
204 |
|
---|
205 |
|
---|
206 | /* ===============================================================================
|
---|
207 | * Function: time_normalise
|
---|
208 | * Description:
|
---|
209 | * If the micro-second component is negative or over a million,
|
---|
210 | * then must take the over/under flow and add it to the second component.
|
---|
211 | * Usage:
|
---|
212 | * Use this routine when you have just been doing straight addition and
|
---|
213 | * subtraction on the micro-second component. For this could lead to a
|
---|
214 | * negative number or over a million.
|
---|
215 | *
|
---|
216 | * =============================================================================== */
|
---|
217 |
|
---|
218 | #ifndef HAVE_TIMES
|
---|
219 |
|
---|
220 | #ifndef __WIN32__ /* [RPAP - Feb 97: WIN32 Port] */
|
---|
221 | static
|
---|
222 | #endif
|
---|
223 | void
|
---|
224 | time_normalise (struct timeval *t)
|
---|
225 | {
|
---|
226 | while (t->tv_usec < 0)
|
---|
227 | {
|
---|
228 | t->tv_usec += MILLION;
|
---|
229 | t->tv_sec--;
|
---|
230 | }
|
---|
231 | while (t->tv_usec > MILLION)
|
---|
232 | {
|
---|
233 | t->tv_usec -= MILLION;
|
---|
234 | t->tv_sec++;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | #endif
|
---|