[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[b85a6c] | 4 | /* $Id: timer.cc,v 1.8 1997-04-25 18:35:11 obachman Exp $ */ |
---|
[32df82] | 5 | |
---|
[0e1846] | 6 | /* |
---|
| 7 | * ABSTRACT - get the computing time |
---|
| 8 | */ |
---|
| 9 | |
---|
[ea986d] | 10 | #include "mod2.h" |
---|
| 11 | |
---|
[0e1846] | 12 | //the mpw timer is quite the same as the dos timer: |
---|
| 13 | #ifdef macintosh |
---|
| 14 | #define MSDOS |
---|
| 15 | #endif |
---|
| 16 | |
---|
| 17 | int timerv = 0; |
---|
[40edb03] | 18 | static double timer_resolution = TIMER_RESOLUTION; |
---|
[ea986d] | 19 | |
---|
| 20 | static double mintime = 0.5; |
---|
| 21 | |
---|
| 22 | void SetTimerResolution(int res) |
---|
| 23 | { |
---|
| 24 | timer_resolution = (double) res; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | void SetMinDisplayTime(double mtime) |
---|
| 28 | { |
---|
| 29 | mintime = mtime; |
---|
| 30 | } |
---|
| 31 | |
---|
[0e1846] | 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> |
---|
[40edb03] | 42 | |
---|
| 43 | #ifdef TIME_WITH_SYS_TIME |
---|
| 44 | # include <time.h> |
---|
[b85a6c] | 45 | # ifdef HAVE_SYS_TIME_H |
---|
| 46 | # include <sys/time.h> |
---|
| 47 | # endif |
---|
[40edb03] | 48 | #else |
---|
[b85a6c] | 49 | # ifdef HAVE_SYS_TIME_H |
---|
| 50 | # include <sys/time.h> |
---|
[40edb03] | 51 | # else |
---|
| 52 | # include <time.h> |
---|
| 53 | # endif |
---|
| 54 | #endif |
---|
[b85a6c] | 55 | #ifdef HAVE_SYS_TIMES_H |
---|
| 56 | #include <sys/times.h> |
---|
| 57 | #endif |
---|
| 58 | |
---|
[40edb03] | 59 | |
---|
[0e1846] | 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 | */ |
---|
| 83 | static clock_t startl; |
---|
| 84 | static clock_t siStartTime; |
---|
| 85 | |
---|
| 86 | /*3 |
---|
| 87 | * temp structure to get the time |
---|
| 88 | */ |
---|
| 89 | static struct tms t_rec; |
---|
| 90 | |
---|
| 91 | /*0 implementation*/ |
---|
| 92 | |
---|
| 93 | int initTimer() |
---|
| 94 | { |
---|
| 95 | times(&t_rec); |
---|
| 96 | siStartTime = t_rec.tms_utime; |
---|
| 97 | return (int)time(NULL); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void 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 | */ |
---|
| 109 | int getTimer() |
---|
| 110 | { |
---|
| 111 | clock_t curr; |
---|
| 112 | |
---|
| 113 | times(&t_rec); |
---|
| 114 | curr = t_rec.tms_utime - siStartTime; |
---|
| 115 | |
---|
[ea986d] | 116 | double f = ((double)curr) * timer_resolution / (double)HZ; |
---|
[0e1846] | 117 | return (int)(f+0.5); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /*2 |
---|
| 121 | * stops timer, writes string s and the time since last call of startTimer |
---|
[ea986d] | 122 | * if this time is > mintime sec |
---|
[0e1846] | 123 | */ |
---|
| 124 | void writeTime(void* v) |
---|
| 125 | { |
---|
| 126 | clock_t curr; |
---|
| 127 | |
---|
| 128 | times(&t_rec); |
---|
| 129 | curr = t_rec.tms_utime - startl; |
---|
| 130 | |
---|
[ea986d] | 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*/ |
---|
| 138 | int rtimerv = 0; |
---|
| 139 | static struct timeval startRl; |
---|
| 140 | static struct timeval siStartRTime; |
---|
| 141 | static struct timezone tzp; |
---|
| 142 | void initRTimer() |
---|
| 143 | { |
---|
| 144 | gettimeofday(&startRl, &tzp); |
---|
[286bd57] | 145 | gettimeofday(&siStartRTime, &tzp); |
---|
[ea986d] | 146 | } |
---|
| 147 | |
---|
| 148 | void startRTimer() |
---|
| 149 | { |
---|
| 150 | gettimeofday(&siStartRTime, &tzp); |
---|
[0e1846] | 151 | } |
---|
[ea986d] | 152 | |
---|
| 153 | /*2 |
---|
| 154 | * returns the time since a fixed point in resolutions |
---|
| 155 | */ |
---|
| 156 | int 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 | */ |
---|
| 179 | void 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 | |
---|
[0e1846] | 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 | */ |
---|
| 215 | static clock_t startl; |
---|
| 216 | static clock_t siStartTime; |
---|
| 217 | |
---|
| 218 | /*2 |
---|
| 219 | * starts the timer; used with getTime |
---|
| 220 | */ |
---|
| 221 | int initTimer() |
---|
| 222 | { |
---|
| 223 | siStartTime = clock(); |
---|
| 224 | return (int)siStartTime; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /*2 |
---|
| 228 | * starts the timer; used with writeTime |
---|
| 229 | */ |
---|
| 230 | void startTimer() |
---|
| 231 | { |
---|
| 232 | startl = clock(); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /*2 |
---|
| 236 | * returns the time since a fixed point in seconds |
---|
| 237 | */ |
---|
| 238 | int getTimer() |
---|
| 239 | { |
---|
| 240 | clock_t curr = clock() - siStartTime; |
---|
[ea986d] | 241 | double f = ((double)curr)*timer_resolution/ (double)CLOCKS_PER_SEC; |
---|
[0e1846] | 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 | */ |
---|
| 248 | void writeTime(void* v) |
---|
| 249 | { |
---|
| 250 | clock_t curr = clock() - startl; |
---|
| 251 | double f = ((double)curr) / CLOCKS_PER_SEC; |
---|
[ea986d] | 252 | if (f > mintime) |
---|
| 253 | Print("//%s %.1f sec\n" ,v ,f); |
---|
[0e1846] | 254 | } |
---|
| 255 | #endif |
---|