[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[85e68dd] | 4 | /* $Id: timer.cc,v 1.6 2008-03-19 17:44:13 Singular Exp $ */ |
---|
[35aab3] | 5 | |
---|
| 6 | /* |
---|
| 7 | * ABSTRACT - get the computing time |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include "mod2.h" |
---|
| 11 | |
---|
| 12 | int timerv = 0; |
---|
| 13 | static double timer_resolution = TIMER_RESOLUTION; |
---|
| 14 | |
---|
| 15 | static double mintime = 0.5; |
---|
| 16 | |
---|
| 17 | void SetTimerResolution(int res) |
---|
| 18 | { |
---|
| 19 | timer_resolution = (double) res; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | void SetMinDisplayTime(double mtime) |
---|
| 23 | { |
---|
| 24 | mintime = mtime; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | #include <stdio.h> |
---|
| 28 | #include <math.h> |
---|
| 29 | #include <unistd.h> |
---|
| 30 | #include <float.h> |
---|
| 31 | #include <mylimits.h> |
---|
| 32 | #include <sys/types.h> |
---|
| 33 | |
---|
| 34 | #ifdef TIME_WITH_SYS_TIME |
---|
| 35 | # include <time.h> |
---|
| 36 | # ifdef HAVE_SYS_TIME_H |
---|
| 37 | # include <sys/time.h> |
---|
| 38 | # endif |
---|
| 39 | #else |
---|
| 40 | # ifdef HAVE_SYS_TIME_H |
---|
| 41 | # include <sys/time.h> |
---|
| 42 | # else |
---|
| 43 | # include <time.h> |
---|
| 44 | # endif |
---|
| 45 | #endif |
---|
[9bbbc8] | 46 | |
---|
[35aab3] | 47 | #ifdef HAVE_SYS_TIMES_H |
---|
| 48 | #include <sys/times.h> |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | // need to be adjusted on your machine: the number of ticks per second: HZ |
---|
| 53 | #ifndef HZ |
---|
| 54 | #include <sys/param.h> |
---|
| 55 | #endif |
---|
[9bbbc8] | 56 | |
---|
[35aab3] | 57 | #if !defined(HZ) && defined(CLOCKS_PER_SEC) |
---|
| 58 | #define HZ CLOCKS_PER_SEC |
---|
| 59 | #endif |
---|
[9bbbc8] | 60 | |
---|
[35aab3] | 61 | #if !defined(HZ) && defined(CLK_TCK) |
---|
| 62 | #define HZ CLK_TCK |
---|
| 63 | #endif |
---|
[9bbbc8] | 64 | |
---|
| 65 | #if !defined(HZ) && defined(HAVE_SYSCONF) |
---|
| 66 | #define HZ sysconf(_SC_CLK_TCK) |
---|
[35aab3] | 67 | #endif |
---|
[9bbbc8] | 68 | |
---|
| 69 | #ifndef HZ |
---|
| 70 | // last resort |
---|
| 71 | #ifdef sun |
---|
| 72 | #define HZ 60.0 |
---|
| 73 | #else |
---|
| 74 | #define HZ 100.0 |
---|
| 75 | #endif |
---|
[35aab3] | 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+t_rec.tms_stime; |
---|
| 97 | return (int)time(NULL); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void 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 | */ |
---|
| 109 | int 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 | */ |
---|
[05504b7] | 124 | #ifdef EXTEND_TIMER_D |
---|
| 125 | extern int iiOp; |
---|
| 126 | #endif |
---|
[85e68dd] | 127 | void writeTime(const char* v) |
---|
[35aab3] | 128 | { |
---|
| 129 | clock_t curr; |
---|
| 130 | |
---|
| 131 | times(&t_rec); |
---|
| 132 | curr = t_rec.tms_utime+t_rec.tms_stime - startl; |
---|
| 133 | |
---|
| 134 | double f = ((double)curr) / (double)HZ; |
---|
| 135 | if (f > mintime) |
---|
[05504b7] | 136 | { |
---|
| 137 | #ifdef EXTEND_TIMER_D |
---|
| 138 | Print("//%s %.2f sec (%d) >>%s<<\n" ,v ,f,iiOp,my_yylinebuf); |
---|
| 139 | #else |
---|
[35aab3] | 140 | Print("//%s %.2f sec\n" ,v ,f); |
---|
[05504b7] | 141 | #endif |
---|
| 142 | } |
---|
[35aab3] | 143 | } |
---|
| 144 | |
---|
| 145 | #ifdef HAVE_RTIMER |
---|
| 146 | /*0 Real timer implementation*/ |
---|
| 147 | int rtimerv = 0; |
---|
| 148 | static struct timeval startRl; |
---|
| 149 | static struct timeval siStartRTime; |
---|
| 150 | static struct timezone tzp; |
---|
| 151 | void initRTimer() |
---|
| 152 | { |
---|
| 153 | gettimeofday(&startRl, &tzp); |
---|
| 154 | gettimeofday(&siStartRTime, &tzp); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void startRTimer() |
---|
| 158 | { |
---|
| 159 | gettimeofday(&siStartRTime, &tzp); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /*2 |
---|
| 163 | * returns the time since a fixed point in resolutions |
---|
| 164 | */ |
---|
| 165 | int getRTimer() |
---|
| 166 | { |
---|
| 167 | struct timeval now; |
---|
| 168 | |
---|
| 169 | gettimeofday(&now, &tzp); |
---|
| 170 | |
---|
| 171 | if (startRl.tv_usec > now.tv_usec) |
---|
| 172 | { |
---|
| 173 | now.tv_usec += 1000000; |
---|
| 174 | now.tv_sec --; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | double f =((double) (now.tv_sec - startRl.tv_sec))*timer_resolution + |
---|
| 178 | ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution / |
---|
| 179 | (double) 1000000; |
---|
| 180 | |
---|
| 181 | return (int)(f+0.5); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /*2 |
---|
| 185 | * stops timer, writes string s and the time since last call of startTimer |
---|
| 186 | * if this time is > mintime |
---|
| 187 | */ |
---|
[85e68dd] | 188 | void writeRTime(const char* v) |
---|
[35aab3] | 189 | { |
---|
| 190 | struct timeval now; |
---|
| 191 | |
---|
| 192 | gettimeofday(&now, &tzp); |
---|
| 193 | |
---|
| 194 | if (siStartRTime.tv_usec > now.tv_usec) |
---|
| 195 | { |
---|
| 196 | now.tv_usec += 1000000; |
---|
| 197 | now.tv_sec --; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | double f =((double) (now.tv_sec - siStartRTime.tv_sec)) + |
---|
| 201 | ((double) (now.tv_usec - siStartRTime.tv_usec)) / |
---|
| 202 | (double) 1000000; |
---|
| 203 | |
---|
| 204 | if (f > mintime) |
---|
| 205 | Print("//%s %.2f sec \n" ,v ,f); |
---|
| 206 | } |
---|
| 207 | #endif |
---|