1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | static char rcsid[] = "$Id: timer.cc,v 1.5 1997-03-28 21:44:40 obachman Exp $"; |
---|
5 | /* |
---|
6 | * ABSTRACT - get the computing time |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | |
---|
11 | //the mpw timer is quite the same as the dos timer: |
---|
12 | #ifdef macintosh |
---|
13 | #define MSDOS |
---|
14 | #endif |
---|
15 | |
---|
16 | int timerv = 0; |
---|
17 | #ifdef TIME_SEC |
---|
18 | static double timer_resolution = 1.0; |
---|
19 | #else |
---|
20 | static double timer_resolution = 10.0; |
---|
21 | #endif |
---|
22 | |
---|
23 | static double mintime = 0.5; |
---|
24 | |
---|
25 | void SetTimerResolution(int res) |
---|
26 | { |
---|
27 | timer_resolution = (double) res; |
---|
28 | } |
---|
29 | |
---|
30 | void SetMinDisplayTime(double mtime) |
---|
31 | { |
---|
32 | mintime = mtime; |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | #ifndef MSDOS |
---|
37 | |
---|
38 | /*tested on HP9000/700, HP9000/300, Linux 0.99, SUN Sparc*/ |
---|
39 | #include <time.h> |
---|
40 | #include <stdio.h> |
---|
41 | #include <math.h> |
---|
42 | #include <unistd.h> |
---|
43 | #include <float.h> |
---|
44 | #include <limits.h> |
---|
45 | #include <sys/types.h> |
---|
46 | #include <sys/times.h> |
---|
47 | // need to be adjusted on your machine: the number of ticks per second: HZ |
---|
48 | #ifndef HZ |
---|
49 | #include <sys/param.h> |
---|
50 | #endif |
---|
51 | #if !defined(HZ) && defined(CLOCKS_PER_SEC) |
---|
52 | #define HZ CLOCKS_PER_SEC |
---|
53 | #endif |
---|
54 | #if !defined(HZ) && defined(CLK_TCK) |
---|
55 | #define HZ CLK_TCK |
---|
56 | #endif |
---|
57 | #ifndef HZ |
---|
58 | #ifdef sun |
---|
59 | #define HZ 60.0 |
---|
60 | #else |
---|
61 | #define HZ 100.0 |
---|
62 | #endif |
---|
63 | #endif |
---|
64 | |
---|
65 | #include "timer.h" |
---|
66 | #include "febase.h" |
---|
67 | /*3 |
---|
68 | * the start time of the timer |
---|
69 | */ |
---|
70 | static clock_t startl; |
---|
71 | static clock_t siStartTime; |
---|
72 | |
---|
73 | /*3 |
---|
74 | * temp structure to get the time |
---|
75 | */ |
---|
76 | static struct tms t_rec; |
---|
77 | |
---|
78 | /*0 implementation*/ |
---|
79 | |
---|
80 | int initTimer() |
---|
81 | { |
---|
82 | times(&t_rec); |
---|
83 | siStartTime = t_rec.tms_utime; |
---|
84 | return (int)time(NULL); |
---|
85 | } |
---|
86 | |
---|
87 | void startTimer() |
---|
88 | { |
---|
89 | times(&t_rec); |
---|
90 | startl = t_rec.tms_utime; |
---|
91 | } |
---|
92 | |
---|
93 | /*2 |
---|
94 | * returns the time since a fixed point in seconds |
---|
95 | */ |
---|
96 | int getTimer() |
---|
97 | { |
---|
98 | clock_t curr; |
---|
99 | |
---|
100 | times(&t_rec); |
---|
101 | curr = t_rec.tms_utime - siStartTime; |
---|
102 | |
---|
103 | double f = ((double)curr) * timer_resolution / (double)HZ; |
---|
104 | return (int)(f+0.5); |
---|
105 | } |
---|
106 | |
---|
107 | /*2 |
---|
108 | * stops timer, writes string s and the time since last call of startTimer |
---|
109 | * if this time is > mintime sec |
---|
110 | */ |
---|
111 | void writeTime(void* v) |
---|
112 | { |
---|
113 | clock_t curr; |
---|
114 | |
---|
115 | times(&t_rec); |
---|
116 | curr = t_rec.tms_utime - startl; |
---|
117 | |
---|
118 | double f = ((double)curr) / (double)HZ; |
---|
119 | if (f > mintime) |
---|
120 | Print("//%s %.2f sec\n" ,v ,f); |
---|
121 | } |
---|
122 | |
---|
123 | #ifdef HAVE_RTIMER |
---|
124 | #include <sys/time.h> |
---|
125 | /*0 Real timer implementation*/ |
---|
126 | int rtimerv = 0; |
---|
127 | static struct timeval startRl; |
---|
128 | static struct timeval siStartRTime; |
---|
129 | static struct timezone tzp; |
---|
130 | void initRTimer() |
---|
131 | { |
---|
132 | gettimeofday(&startRl, &tzp); |
---|
133 | gettimeofday(&siStartRTime, &tzp); |
---|
134 | } |
---|
135 | |
---|
136 | void startRTimer() |
---|
137 | { |
---|
138 | gettimeofday(&siStartRTime, &tzp); |
---|
139 | } |
---|
140 | |
---|
141 | /*2 |
---|
142 | * returns the time since a fixed point in resolutions |
---|
143 | */ |
---|
144 | int getRTimer() |
---|
145 | { |
---|
146 | struct timeval now; |
---|
147 | |
---|
148 | gettimeofday(&now, &tzp); |
---|
149 | |
---|
150 | if (startRl.tv_usec > now.tv_usec) |
---|
151 | { |
---|
152 | now.tv_usec += 1000000; |
---|
153 | now.tv_sec --; |
---|
154 | } |
---|
155 | |
---|
156 | double f =((double) (now.tv_sec - startRl.tv_sec))*timer_resolution + |
---|
157 | ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution / |
---|
158 | (double) 1000000; |
---|
159 | |
---|
160 | return (int)(f+0.5); |
---|
161 | } |
---|
162 | |
---|
163 | /*2 |
---|
164 | * stops timer, writes string s and the time since last call of startTimer |
---|
165 | * if this time is > mintime |
---|
166 | */ |
---|
167 | void writeRTime(void* v) |
---|
168 | { |
---|
169 | struct timeval now; |
---|
170 | |
---|
171 | gettimeofday(&now, &tzp); |
---|
172 | |
---|
173 | if (siStartRTime.tv_usec > now.tv_usec) |
---|
174 | { |
---|
175 | now.tv_usec += 1000000; |
---|
176 | now.tv_sec --; |
---|
177 | } |
---|
178 | |
---|
179 | double f =((double) (now.tv_sec - siStartRTime.tv_sec)) + |
---|
180 | ((double) (now.tv_usec - siStartRTime.tv_usec)) / |
---|
181 | (double) 1000000; |
---|
182 | |
---|
183 | if (f > mintime) |
---|
184 | Print("//%s %.2f sec \n" ,v ,f); |
---|
185 | } |
---|
186 | #endif |
---|
187 | |
---|
188 | #else |
---|
189 | |
---|
190 | /*tested on MSDOS-GCC, Macintosh*/ |
---|
191 | #include <time.h> |
---|
192 | #include <stdio.h> |
---|
193 | #include <math.h> |
---|
194 | #include <float.h> |
---|
195 | |
---|
196 | #include "mod2.h" |
---|
197 | #include "timer.h" |
---|
198 | #include "febase.h" |
---|
199 | |
---|
200 | /*3 |
---|
201 | * the start time of the timer |
---|
202 | */ |
---|
203 | static clock_t startl; |
---|
204 | static clock_t siStartTime; |
---|
205 | |
---|
206 | /*2 |
---|
207 | * starts the timer; used with getTime |
---|
208 | */ |
---|
209 | int initTimer() |
---|
210 | { |
---|
211 | siStartTime = clock(); |
---|
212 | return (int)siStartTime; |
---|
213 | } |
---|
214 | |
---|
215 | /*2 |
---|
216 | * starts the timer; used with writeTime |
---|
217 | */ |
---|
218 | void startTimer() |
---|
219 | { |
---|
220 | startl = clock(); |
---|
221 | } |
---|
222 | |
---|
223 | /*2 |
---|
224 | * returns the time since a fixed point in seconds |
---|
225 | */ |
---|
226 | int getTimer() |
---|
227 | { |
---|
228 | clock_t curr = clock() - siStartTime; |
---|
229 | double f = ((double)curr)*timer_resolution/ (double)CLOCKS_PER_SEC; |
---|
230 | return (int)(f+0.5); |
---|
231 | } |
---|
232 | /*2 |
---|
233 | * stops timer, writes string s and the time since last call of startTimer |
---|
234 | * if this time is > 0.5 sec |
---|
235 | */ |
---|
236 | void writeTime(void* v) |
---|
237 | { |
---|
238 | clock_t curr = clock() - startl; |
---|
239 | double f = ((double)curr) / CLOCKS_PER_SEC; |
---|
240 | if (f > mintime) |
---|
241 | Print("//%s %.1f sec\n" ,v ,f); |
---|
242 | } |
---|
243 | #endif |
---|