00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include "bc_machdefs.h"
00071 #include "bc_util.h"
00072
00073 #ifdef HAVE_GETRUSAGE
00074
00075 #ifdef HAVE_SYS_RESOURCE_H
00076 # include <sys/resource.h>
00077 #endif
00078
00079 double CCutil_zeit (void)
00080 {
00081 struct rusage ru;
00082
00083 getrusage (RUSAGE_SELF, &ru);
00084
00085 return ((double) ru.ru_utime.tv_sec) +
00086 ((double) ru.ru_utime.tv_usec) / 1000000.0;
00087 }
00088 #else
00089
00090 #ifdef HAVE_TIMES
00091
00092 #ifdef HAVE_SYS_PARAM_H
00093 # include <sys/param.h>
00094 #endif
00095 #ifdef HAVE_SYS_TIMES_H
00096 # include <sys/times.h>
00097 #endif
00098
00099 #ifdef CLK_TCK
00100 #define MACHINE_FREQ CLK_TCK
00101 #else
00102 #define MACHINE_FREQ HZ
00103 #endif
00104
00105 double CCutil_zeit (void)
00106 {
00107 struct tms now;
00108
00109 times (&now);
00110 return ((double) now.tms_utime) / ((double) MACHINE_FREQ);
00111 }
00112 #else
00113
00114 #ifdef HAVE_CLOCK
00115
00116 #ifndef CLOCKS_PER_SEC
00117 #ifdef CLK_TCK
00118 #define CLOCKS_PER_SEC CLK_TCK
00119 #else
00120 #define CLOCKS_PER_SEC 60
00121 #endif
00122 #endif
00123
00124 double CCutil_zeit (void)
00125 {
00126 return ((double) clock ()) / ((double) CLOCKS_PER_SEC);
00127 }
00128
00129 #else
00130
00131 double CCutil_zeit (void)
00132 {
00133 return 0.0;
00134 }
00135 #endif
00136 #endif
00137 #endif
00138
00139 double CCutil_real_zeit (void)
00140 {
00141 double res = time (0);
00142 return res;
00143 }
00144
00145 void CCutil_init_timer (CCutil_timer * t,
00146 const char *name)
00147 {
00148 t->szeit = -1.0;
00149 t->cum_zeit = 0.0;
00150 t->count = 0;
00151 if (name == (char *) NULL || name[0] == '\0')
00152 {
00153 strncpy (t->name, "ANONYMOUS", sizeof (t->name) - 1);
00154 }
00155 else
00156 {
00157 strncpy (t->name, name, sizeof (t->name) - 1);
00158 }
00159 t->name[sizeof (t->name) - 1] = '\0';
00160 }
00161
00162 void CCutil_start_timer (CCutil_timer * t)
00163 {
00164 if (fabs (t->szeit + 1.0) < 1e-3)
00165 {
00166 fprintf (stderr, "Warning: restarting running timer %s\n", t->name);
00167 }
00168 t->szeit = CCutil_zeit ();
00169 }
00170
00171 void CCutil_suspend_timer (CCutil_timer * t)
00172 {
00173 if (fabs (t->szeit + 1.0) < 1e-3)
00174 {
00175 fprintf (stderr, "Warning: suspended non-running timer %s\n", t->name);
00176 return;
00177 }
00178
00179 t->cum_zeit += CCutil_zeit () - t->szeit;
00180 t->szeit = -1.0;
00181 }
00182
00183 void CCutil_resume_timer (CCutil_timer * t)
00184 {
00185 if (fabs (t->szeit + 1.0) < 1e-3)
00186 {
00187 fprintf (stderr, "Warning: resuming running timer %s\n", t->name);
00188 return;
00189 }
00190 t->szeit = CCutil_zeit ();
00191 }
00192
00193 double CCutil_stop_timer (CCutil_timer * t,
00194 int printit)
00195 {
00196 double z;
00197
00198 if (fabs (t->szeit + 1.0) < 1e-3)
00199 {
00200 fprintf (stderr, "Warning: stopping non-running timer %s\n", t->name);
00201 return 0.0;
00202 }
00203 z = CCutil_zeit () - t->szeit;
00204 t->szeit = -1.0;
00205 t->cum_zeit += z;
00206 t->count++;
00207 if (printit == 1 || (printit == 2 && z > 0.0))
00208 {
00209 printf ("Time for %s: %.2f seconds (%.2f total in %d calls)\n",
00210 t->name, z, t->cum_zeit, t->count);
00211 fflush (stdout);
00212 }
00213 else if (printit == 3 || (printit == 4 && z > 0.0))
00214 {
00215 printf ("T %-34.34s %9.2f %9.2f %d\n", t->name, z, t->cum_zeit, t->count);
00216 fflush (stdout);
00217 }
00218 return z;
00219 }
00220
00221 double CCutil_total_timer (CCutil_timer * t,
00222 int printit)
00223 {
00224 double z = t->cum_zeit;
00225
00226 if (fabs (t->szeit + 1.0) < 1e-3)
00227 z += CCutil_zeit () - t->szeit;
00228 if (printit == 1 || (printit == 2 && z > 0.0))
00229 {
00230 printf ("Total time for %-34.34s %.2f seconds in %d%s calls\n",
00231 t->name, z, t->count, (fabs (t->szeit + 1.0) < 1e-3) ? "" : "+1");
00232 fflush (stdout);
00233 }
00234 else if (printit == 3 || (printit == 4 && z > 0.0))
00235 {
00236 printf ("CT %-34.34s %9.2f %6d%s\n",
00237 t->name, z, t->count, (fabs (t->szeit + 1.0) < 1e-3) ? "" : "+1");
00238 fflush (stdout);
00239 }
00240 return z;
00241 }