source: trunk/indexers/mg/lib/timing.c@ 13654

Last change on this file since 13654 was 13654, checked in by kjdon, 17 years ago

tidied up the top comments, removed Ids, and old log messages

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
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 **************************************************************************/
21
22#include "sysfuncs.h"
23#include "timing.h"
24
25
26/*
27 * Return the time in seconds since 00:00:00 GMT, Jan. 1, 1970 to the
28 * best precision possible
29 *
30 */
31double
32RealTime (void)
33{
34 return ((double) time (NULL));
35}
36
37
38
39/*
40 * Return the amount of system and user CPU used by the process to date to
41 * the best precision possible. If user is non-null then it is initialised to
42 * the user time. If sys is non-null then it is initialised to the system time.
43 *
44 */
45double
46CPUTime (double *user, double *sys)
47{
48
49#if defined(HAVE_TIMES)
50
51 struct tms buffer;
52 static double clk_tck = 0;
53 double u, s;
54
55 times (&buffer);
56
57 if (clk_tck == 0)
58 clk_tck = CLK_TCK;
59
60 u = (double) buffer.tms_utime / clk_tck;
61 s = (double) buffer.tms_stime / clk_tck;
62 if (user)
63 *user = u;
64 if (sys)
65 *sys = s;
66 return u + s;
67
68#elif defined(HAVE_GETRUSAGE)
69
70 struct rusage ruse;
71 getrusage (RUSAGE_SELF, &ruse);
72 if (user)
73 *user = (double) ruse.ru_utime.tv_sec +
74 (double) ruse.ru_utime.tv_usec / 1000000;
75 if (sys)
76 *sys = (double) ruse.ru_stime.tv_sec +
77 (double) ruse.ru_stime.tv_usec / 1000000;
78 return ((double) ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec +
79 ((double) ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) / 1000000);
80
81#else
82
83 /* [RPAP - Feb 97: WIN32 Port] */
84#ifdef __WIN32__
85 if (user) *user = 0.0;
86 if (sys) *sys = 0.0;
87 return 0.0;
88#else
89 -->Can NOT find a system time routine to use ! !!
90#endif /* __WIN32__ */
91
92#endif
93
94}
95
96
97
98
99/*
100 * Get the Real and CPU time and store them in a ProgTime structure
101 */
102void
103GetTime (ProgTime * StartTime)
104{
105 StartTime->RealTime = RealTime ();
106 StartTime->CPUTime = CPUTime (NULL, NULL);
107}
108
109
110
111
112/*
113 * Display the Real and CPU time elapsed since the StartTime anf FinishTime
114 * structures were initialised. If FinishTime is NULL then FinishTime is
115 * Now.
116 */
117char *
118ElapsedTime (ProgTime * StartTime,
119 ProgTime * FinishTime)
120{
121 static char buf[50];
122 double Real, CPU, hour, min, sec;
123 if (!FinishTime)
124 {
125 Real = RealTime () - StartTime->RealTime;
126 CPU = CPUTime (NULL, NULL) - StartTime->CPUTime;
127 }
128 else
129 {
130 Real = FinishTime->RealTime - StartTime->RealTime;
131 CPU = FinishTime->CPUTime - StartTime->CPUTime;
132 }
133 hour = floor (CPU / 3600);
134 min = floor ((CPU - hour * 3600) / 60);
135 sec = CPU - hour * 3600 - min * 60;
136 sprintf (buf, "%02.0f:%02.0f:%05.2f cpu, %02d:%02d:%02d elapsed.",
137 hour, min, sec,
138 ((int) ceil (Real)) / 3600,
139 (((int) ceil (Real)) % 3600) / 60, ((int) ceil (Real)) % 60);
140 return (buf);
141}
142
143
144#define MILLION 1000000
145
146/* ===============================================================================
147 * Function: cputime_string
148 * Description:
149 * Prints out given cpu time into a string buffer.
150 * Input: cpu-time in clock_t or timeval format
151 * Output: pointer to internal buffer
152 * =============================================================================== */
153
154#ifdef HAVE_TIMES
155char *
156cputime_string (clock_t clk)
157{
158 static char buf[15];
159 double CPU, hour, min, sec;
160
161 CPU = (double) clk / (double) CLK_TCK;
162 hour = floor (CPU / 3600);
163 min = floor ((CPU - hour * 3600) / 60);
164 sec = CPU - hour * 3600 - min * 60;
165 sprintf (buf, "%02.0f:%02.0f:%05.2f", hour, min, sec);
166 return (buf);
167}
168#else
169char *
170cputime_string (struct timeval *t)
171{
172 static char buf[15];
173 double CPU, hour, min, sec;
174
175 CPU = (double) t->tv_sec + (double) t->tv_usec / MILLION;
176 hour = floor (CPU / 3600);
177 min = floor ((CPU - hour * 3600) / 60);
178 sec = CPU - hour * 3600 - min * 60;
179 sprintf (buf, "%02.0f:%02.0f:%05.2f", hour, min, sec);
180 return (buf);
181}
182#endif
183
184
185
186/* ===============================================================================
187 * Function: time_normalise
188 * Description:
189 * If the micro-second component is negative or over a million,
190 * then must take the over/under flow and add it to the second component.
191 * Usage:
192 * Use this routine when you have just been doing straight addition and
193 * subtraction on the micro-second component. For this could lead to a
194 * negative number or over a million.
195 *
196 * =============================================================================== */
197
198#ifndef HAVE_TIMES
199
200#ifndef __WIN32__ /* [RPAP - Feb 97: WIN32 Port] */
201static
202#endif
203void
204time_normalise (struct timeval *t)
205{
206 while (t->tv_usec < 0)
207 {
208 t->tv_usec += MILLION;
209 t->tv_sec--;
210 }
211 while (t->tv_usec > MILLION)
212 {
213 t->tv_usec -= MILLION;
214 t->tv_sec++;
215 }
216}
217#endif
Note: See TracBrowser for help on using the repository browser.