source: git/Singular/timer.cc @ a70441f

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