File: chrono.c

package info (click to toggle)
testu01 1.2.3%2Bds1-3
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 17,748 kB
  • sloc: ansic: 52,357; makefile: 248; sh: 53
file content (226 lines) | stat: -rw-r--r-- 5,798 bytes parent folder | download | duplicates (4)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*************************************************************************\
 *
 * Package:        MyLib
 * File:           chrono.c
 * Environment:    ANSI C
 *
 * Copyright (c) 2002 Pierre L'Ecuyer, DIRO, Université de Montréal.
 * e-mail: lecuyer@iro.umontreal.ca
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted without a fee for private, research,
 * academic, or other non-commercial purposes.
 * Any use of this software in a commercial environment requires a
 * written licence from the copyright owner.
 *
 * Any changes made to this package must be clearly identified as such.
 *
 * In scientific publications which used this software, a reference to it
 * would be appreciated.
 *
 * Redistributions of source code must retain this copyright notice
 * and the following disclaimer.
 *
 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
\*************************************************************************/

#ifdef HAVE_CONFIG_H
#include "gdefconf.h"
#endif

#include "chrono.h"
#include "gdef.h"
#include "util.h"
#include "num.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_WINDOWS_H

#include <windows.h>

static HANDLE currentProcess = NULL;

/*
 * A helper function for converting FILETIME to a LONGLONG [safe from memory
 * alignment point of view].
 */
static ULONGLONG
fileTimeToInt64 (const FILETIME * time)
{
    ULARGE_INTEGER _time;
    _time.LowPart = time->dwLowDateTime;
    _time.HighPart = time->dwHighDateTime;
    return _time.QuadPart;
}

static void Heure (unsigned long *tsec, unsigned long *tusec) {
   if (currentProcess == NULL)
      currentProcess = GetCurrentProcess();
   FILETIME creationTime, exitTime, kernelTime, userTime;
   /* Strongly inspired from
    * http://www.javaworld.com/javaworld/javaqa/2002-11/01-qa-1108-cpu.html */
   GetProcessTimes (currentProcess, &creationTime, &exitTime,
		   &kernelTime, &userTime);
   ULONGLONG rawTime = fileTimeToInt64 (&kernelTime) +
     fileTimeToInt64 (&userTime);
   /* We have to divide by 10000 to get milliseconds out of
    * the computed time */
   *tsec = (unsigned long)(rawTime / 10000000);
   *tusec = (unsigned long)((rawTime % 10000000) / 10);
}


#elif defined(USE_ANSI_CLOCK)
/* ANSI C timer */

static void Heure (
   unsigned long *tsec,            /* Seconds */
   unsigned long *tmicrosec        /* Micro-seconds */
   )
/* 
 * Function returning the CPU time used by a program since it was
 * started. This function is ANSI C compliant.
 */
{
   clock_t t;
   double y;

   t = clock ();
   y = ((double) t) / CLOCKS_PER_SEC;
   *tsec = y;
   *tmicrosec = (y - *tsec) * 1000000;
}


#else
/* POSIX timer */

#include <sys/times.h>
#include <unistd.h>

static void Heure (unsigned long *tsec, unsigned long *tusec)
/*
 * Function returning the CPU time used by a program since it was
 * started. This function is NOT ANSI C compliant.
 */
{
   struct tms us;
   long TICKS, z;

   TICKS = sysconf(_SC_CLK_TCK);
   if (TICKS == -1) {
     fprintf (stdout, "chrono.c:   'sysconf(_SC_CLK_TCK)' failed\n");
   }
   z = times (&us);
   if (z == -1) {
      fprintf (stdout, "chrono.c:   timer times failed\n");
   }

   /* CPU time = user time + system time */
   *tusec = us.tms_utime + us.tms_stime;

   *tsec = *tusec / TICKS;
   *tusec = (*tusec % TICKS) * 1000000 / TICKS;
}

#endif


/*------------------------------------------------------------------------*/

void chrono_Init (chrono_Chrono *C)
{
   Heure (&C->second, &C->microsec);
}


chrono_Chrono * chrono_Create (void)
{
   chrono_Chrono *C;
   C = (chrono_Chrono *) util_Malloc (sizeof (chrono_Chrono));
   Heure (&C->second, &C->microsec);
   return C;
}


void chrono_Delete (chrono_Chrono *C)
{
   util_Free (C);
}


double chrono_Val (chrono_Chrono *C, chrono_TimeFormat Unit)
{
   double temps;                     /* Time elapsed, in seconds */
   chrono_Chrono now;
   Heure (&now.second, &now.microsec);
   temps = (((double) now.microsec - (double) C->microsec) / 1.E+6 +
             (double) now.second) - (double) C->second;

   switch (Unit) {
   case chrono_sec:
      return temps;
   case chrono_min:
      return temps * 1.666666667E-2;
   case chrono_hours:
      return temps * 2.777777778E-4;
   case chrono_days:
      return temps * 1.157407407E-5;
   case chrono_hms:
      util_Error ("chrono_Val : hms is a wrong arg for chrono_TimeUnit");
   }
   return 0.0;
}

void chrono_Write (chrono_Chrono * C, chrono_TimeFormat Form)
{
   long centieme;
   long minute;
   long heure;
   long seconde;
   double temps;
   if (Form != chrono_hms)
      temps = chrono_Val (C, Form);
   else
      temps = 0.0;
   switch (Form) {
   case chrono_sec:
      num_WriteD (temps, 10, 2, 1);
      printf (" seconds");
      break;
   case chrono_min:
      num_WriteD (temps, 10, 2, 1);
      printf (" minutes");
      break;
   case chrono_hours:
      num_WriteD (temps, 10, 2, 1);
      printf (" hours");
      break;
   case chrono_days:
      num_WriteD (temps, 10, 2, 1);
      printf (" days");
      break;
   case chrono_hms:
      temps = chrono_Val (C, chrono_sec);
      heure = (long) (temps * 2.777777778E-4);
      if (heure > 0)
         temps -= (double) (heure) * 3600.0;
      minute = (long) (temps * 1.666666667E-2);
      if (minute > 0)
         temps -= (double) (minute) * 60.0;
      seconde = (long) (temps);
      centieme = (long) (100.0 * (temps - (double) (seconde)));
      printf ("%02ld:", heure);
      printf ("%02ld:", minute);
      printf ("%02ld.", seconde);
      printf ("%02ld", centieme);
      break;
   }
}