source: git/kernel/timer.cc @ 07625cb

spielwiese
Last change on this file since 07625cb was 05504b7, checked in by Hans Schönemann <hannes@…>, 17 years ago
*hannes: debug stuff git-svn-id: file:///usr/local/Singular/svn/trunk@9819 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: timer.cc,v 1.3 2007-02-07 10:44:44 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
14int        timerv = 0;
15static double timer_resolution = TIMER_RESOLUTION;
16
17static double mintime = 0.5;
18
19void SetTimerResolution(int res)
20{
21  timer_resolution = (double) res;
22}
23
24void 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*/
80static clock_t startl;
81static clock_t siStartTime;
82
83/*3
84* temp structure to get the time
85*/
86static struct tms t_rec;
87
88/*0 implementation*/
89
90int initTimer()
91{
92  times(&t_rec);
93  siStartTime = t_rec.tms_utime+t_rec.tms_stime;
94  return (int)time(NULL);
95}
96
97void 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*/
106int 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#ifdef EXTEND_TIMER_D
122extern int iiOp;
123#endif
124void writeTime(char* v)
125{
126  clock_t curr;
127
128  times(&t_rec);
129  curr = t_rec.tms_utime+t_rec.tms_stime - startl;
130
131  double f =  ((double)curr) / (double)HZ;
132  if (f > mintime)
133  {
134#ifdef EXTEND_TIMER_D
135    Print("//%s %.2f sec (%d) >>%s<<\n" ,v ,f,iiOp,my_yylinebuf);
136#else
137    Print("//%s %.2f sec\n" ,v ,f);
138#endif
139  }
140}
141
142#ifdef HAVE_RTIMER
143/*0 Real timer implementation*/
144int rtimerv = 0;
145static struct timeval  startRl;
146static struct timeval  siStartRTime;
147static struct timezone tzp;
148void initRTimer()
149{
150  gettimeofday(&startRl, &tzp);
151  gettimeofday(&siStartRTime, &tzp);
152}
153
154void startRTimer()
155{
156  gettimeofday(&siStartRTime, &tzp);
157}
158
159/*2
160* returns the time since a fixed point in resolutions
161*/
162int getRTimer()
163{
164  struct timeval now;
165
166  gettimeofday(&now, &tzp);
167
168  if (startRl.tv_usec > now.tv_usec)
169  {
170    now.tv_usec += 1000000;
171    now.tv_sec --;
172  }
173
174  double f =((double)  (now.tv_sec - startRl.tv_sec))*timer_resolution +
175    ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution /
176    (double) 1000000;
177
178  return (int)(f+0.5);
179}
180
181/*2
182* stops timer, writes string s and the time since last call of startTimer
183* if this time is > mintime
184*/
185void writeRTime(char* v)
186{
187  struct timeval now;
188
189  gettimeofday(&now, &tzp);
190
191  if (siStartRTime.tv_usec > now.tv_usec)
192  {
193    now.tv_usec += 1000000;
194    now.tv_sec --;
195  }
196
197  double f =((double)  (now.tv_sec - siStartRTime.tv_sec)) +
198    ((double) (now.tv_usec - siStartRTime.tv_usec)) /
199    (double) 1000000;
200
201  if (f > mintime)
202   Print("//%s %.2f sec \n" ,v ,f);
203}
204#endif
205
206#else /* #defined MSDOS */
207
208/*tested on MSDOS-GCC, Macintosh*/
209#include <time.h>
210#include <stdio.h>
211#include <math.h>
212#include <float.h>
213
214#include "timer.h"
215#include "febase.h"
216
217/*3
218* the start time of the timer
219*/
220static clock_t startl;
221static clock_t siStartTime;
222
223/*2
224* starts the timer; used with getTime
225*/
226int initTimer()
227{
228  siStartTime = clock();
229  return (int)siStartTime;
230}
231
232/*2
233* starts the timer; used with writeTime
234*/
235void startTimer()
236{
237  startl = clock();
238}
239
240/*2
241* returns the time since a fixed point in seconds
242*/
243int getTimer()
244{
245  clock_t curr = clock() - siStartTime;
246  double f =  ((double)curr)*timer_resolution/ (double)CLOCKS_PER_SEC;
247  return (int)(f+0.5);
248}
249/*2
250* stops timer, writes string s and the time since last call of startTimer
251* if this time is > 0.5 sec
252*/
253void writeTime(void* v)
254{
255  clock_t curr = clock() - startl;
256  double f =  ((double)curr) / CLOCKS_PER_SEC;
257  if (f > mintime)
258    Print("//%s %.1f sec\n" ,v ,f);
259}
260#endif /* MSDOS */
Note: See TracBrowser for help on using the repository browser.