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