source: git/Singular/timer.cc @ 400884

spielwiese
Last change on this file since 400884 was b85a6c, checked in by Olaf Bachmann <obachman@…>, 27 years ago
Fri Apr 25 16:59:31 1997 Olaf Bachmann <obachman@ratchwum.mathematik.uni-kl.de (Olaf Bachmann)> * fixed sys/times.h and sys/time.h confusion git-svn-id: file:///usr/local/Singular/svn/trunk@198 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.8 1997-04-25 18:35:11 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 macintosh
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;
97  return (int)time(NULL);
98}
99
100void startTimer()
101{
102  times(&t_rec);
103  startl = t_rec.tms_utime;
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 - 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(void* v)
125{
126  clock_t curr;
127
128  times(&t_rec);
129  curr = t_rec.tms_utime - 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(void* 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
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 "mod2.h"
209#include "timer.h"
210#include "febase.h"
211
212/*3
213* the start time of the timer
214*/
215static clock_t startl;
216static clock_t siStartTime;
217
218/*2
219* starts the timer; used with getTime
220*/
221int initTimer()
222{
223  siStartTime = clock();
224  return (int)siStartTime;
225}
226
227/*2
228* starts the timer; used with writeTime
229*/
230void startTimer()
231{
232  startl = clock();
233}
234
235/*2
236* returns the time since a fixed point in seconds
237*/
238int getTimer()
239{
240  clock_t curr = clock() - siStartTime;
241  double f =  ((double)curr)*timer_resolution/ (double)CLOCKS_PER_SEC;
242  return (int)(f+0.5);
243}
244/*2
245* stops timer, writes string s and the time since last call of startTimer
246* if this time is > 0.5 sec
247*/
248void writeTime(void* v)
249{
250  clock_t curr = clock() - startl;
251  double f =  ((double)curr) / CLOCKS_PER_SEC;
252  if (f > mintime)
253    Print("//%s %.1f sec\n" ,v ,f);
254}
255#endif
Note: See TracBrowser for help on using the repository browser.