bc_zeit.c

Go to the documentation of this file.
00001 /****************************************************************************/
00002 /*                                                                          */
00003 /*  This file is part of CONCORDE                                           */
00004 /*                                                                          */
00005 /*  (c) Copyright 1995--1999 by David Applegate, Robert Bixby,              */
00006 /*  Vasek Chvatal, and William Cook                                         */
00007 /*                                                                          */
00008 /*  Permission is granted for academic research use.  For other uses,       */
00009 /*  contact the authors for licensing options.                              */
00010 /*                                                                          */
00011 /*  Use at your own risk.  We make no guarantees about the                  */
00012 /*  correctness or usefulness of this code.                                 */
00013 /*                                                                          */
00014 /****************************************************************************/
00015 
00016 /****************************************************************************/
00017 /*                                                                          */
00018 /*                        TIMING FUNCTIONS                                  */
00019 /*                                                                          */
00020 /*                            TSP CODE                                      */
00021 /*                                                                          */
00022 /*                                                                          */
00023 /*  Written by:  Applegate, Bixby, Chvatal, and Cook                        */
00024 /*  Date: Summer 1994  (cofeb16)                                            */
00025 /*        December 1997 (dla)                                               */
00026 /*                                                                          */
00027 /*                                                                          */
00028 /*    EXPORTED FUNCTIONS:                                                   */
00029 /*                                                                          */
00030 /*  double CCutil_zeit (void)                                               */
00031 /*        - To measure cpu time.                                            */
00032 /*    To use this, set double t = CCutil_zeit (), run the function you      */
00033 /*    want to time, then compute CCutil_zeit () - t.                        */
00034 /*                                                                          */
00035 /*  double CCutil_real_zeit (void)                                          */
00036 /*    - To measure wall clock time.                                         */
00037 /*                                                                          */
00038 /*    To use this, set double t = CCutil_real_zeit (), run the function     */
00039 /*    you want to time, then compute CCutil_real_zeit () - t.               */
00040 /*                                                                          */
00041 /*  void CCutil_init_timer (CCutil_timer *t, const char *name)              */
00042 /*    - Initializes a CCutil_timer, and gives it a name.                    */
00043 /*    - The name is silently truncated if it is too long.                   */
00044 /*                                                                          */
00045 /*  void CCutil_start_timer (CCutil_timer *t)                               */
00046 /*    - Starts the timer.                                                   */
00047 /*                                                                          */
00048 /*  void CCutil_suspend_timer (CCutil_timer *t)                             */
00049 /*    - Suspends the timer.  Similar to CCutil_stop_timer, but doesn't      */
00050 /*      count a call, and doesn't output.                                   */
00051 /*                                                                          */
00052 /*  void CCutil_resume_timer (CCutil_timer *t)                              */
00053 /*    - Resumes the timer after a suspend.                                  */
00054 /*                                                                          */
00055 /*  double CCutil_stop_timer (CCutil_timer *t, int printit)                 */
00056 /*    - Stops the timer, and returns the time since the last start.         */
00057 /*    - if printit == 1, outputs the time spent.                            */
00058 /*    - if printit == 2, outputs the time spent only if nonzero             */
00059 /*    - if printit == 3,4, like 1,2, except brief, table-form output        */
00060 /*                                                                          */
00061 /*  double CCutil_total_timer (CCutil_timer *t, int printit)                */
00062 /*    - Returns the cumulative time for this timer.                         */
00063 /*    - if printit == 1, outputs the cumulative time.                       */
00064 /*    - if printit == 2, outputs the cumulative time only if nonzero        */
00065 /*    - if printit == 3,4, like 1,2, except brief, table-form output        */
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 /* HAVE_GETRUSAGE */
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 /* HAVE_TIMES */
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 /* HAVE_CLOCK */
00130 
00131 double CCutil_zeit (void)
00132 {
00133   return 0.0;
00134 }
00135 #endif /* HAVE_CLOCK */
00136 #endif /* HAVE_TIMES */
00137 #endif /* HAVE_GETRUSAGE */
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 }

Generated on Thu Oct 20 14:58:40 2005 for DominoParitySeparator by  doxygen 1.4.5