core/vul/vul_timer.cxx
Go to the documentation of this file.
00001 // This is core/vul/vul_timer.cxx
00002 #include "vul_timer.h"
00003 //:
00004 // \file
00005 //
00006 // Copyright (C) 1991 Texas Instruments Incorporated.
00007 //
00008 // Permission is granted to any individual or institution to use, copy, modify,
00009 // and distribute this software, provided that this complete copyright and
00010 // permission notice is maintained, intact, in all copies and supporting
00011 // documentation.
00012 //
00013 // Texas Instruments Incorporated provides this software "as is" without
00014 // express or implied warranty.
00015 //
00016 // Created: BMK 07/14/89  Initial design and implementation
00017 // Updated: LGO 09/23/89  Conform to COOL coding style
00018 // Updated: AFM 12/31/89  OS/2 port
00019 // Updated: DLS 03/22/91  New lite version
00020 // Updated: VDN 10/14/93  ANSI C does not have user/system time.
00021 //
00022 // The Timer class provides timing code  for performance evaluation.  This code
00023 // was originally written by Joe Rahmeh at UT Austin.
00024 //
00025 //  User time:
00026 //    time cpu spends in user mode on behalf of the program.
00027 //  System time:
00028 //    time cpu spends in system mode on behalf of the program.
00029 //  Real time:
00030 //    what you get from a stop watch timer.
00031 //
00032 
00033 #include <vcl_ctime.h>
00034 #if defined(como4301) && defined(__linux__)
00035 # include <sys/types.h>
00036 # include <sys/select.h>
00037 # define __USE_BSD
00038 #endif
00039 #include <vcl_sys/time.h>
00040 # undef __USE_BSD
00041 
00042 struct vul_timer_data
00043 {
00044 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00045   tms usage0;                    // usage mark.
00046   struct timeval real0;          // wall clock mark.
00047 #else
00048  vcl_clock_t usage0;
00049 # if defined(VCL_BORLAND)
00050  struct timeb real0;
00051 # else
00052  struct _timeb real0;
00053 # endif
00054 #endif
00055 };
00056 
00057 #include <vxl_config.h> // VXL_TWO_ARG_GETTIME
00058 
00059 #include <vcl_climits.h>   // for CLK_TCK
00060 #include <vcl_iostream.h>
00061 
00062 
00063 //#define CLK_TCK _sysconf(3) in <limits.h> has error
00064 
00065 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00066 #include <direct.h> // for sysconf()
00067 #else
00068 #include <unistd.h>
00069 #endif
00070 #undef CLK_TCK
00071 #define CLK_TCK sysconf(_SC_CLK_TCK)
00072 
00073 vul_timer::vul_timer()
00074   : data(new vul_timer_data)
00075 {
00076   mark();
00077 }
00078 
00079 vul_timer::~vul_timer()
00080 {
00081   delete data;
00082   data = 0;
00083 }
00084 
00085 //: Sets the reference time to now.
00086 
00087 void vul_timer::mark()
00088 {
00089 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00090   times(&data->usage0);  // user/system time
00091 #ifndef SYSV
00092   struct timezone tz;
00093   gettimeofday(&data->real0, &tz);  // wall clock time
00094 #else
00095 #if VXL_TWO_ARG_GETTIME
00096   gettimeofday(&data->real0, (struct timezone*)0);
00097 #else
00098   gettimeofday(&data->real0);
00099 #endif
00100 #endif
00101 #else
00102   // Win32 section
00103   data->usage0 = vcl_clock();
00104 # if defined(VCL_BORLAND)
00105   ftime(&data->real0);
00106 # else
00107   _ftime(&data->real0);
00108 # endif
00109 #endif
00110 }
00111 
00112 //: Returns the number of milliseconds of wall clock time, since last mark().
00113 
00114 long vul_timer::real()
00115 {
00116   long s;
00117 
00118 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00119   struct timeval  real_time;    // new real time
00120 #ifndef SYSV
00121   struct timezone tz;
00122   gettimeofday(&real_time, &tz);  // wall clock time
00123 #else
00124 #if VXL_TWO_ARG_GETTIME
00125   gettimeofday(&real_time, (struct timezone*)0);
00126 #else
00127   gettimeofday(&real_time);
00128 #endif
00129 #endif
00130   s  = real_time.tv_sec    - data->real0.tv_sec;
00131   long us = real_time.tv_usec - data->real0.tv_usec;
00132 
00133   if (us < 0) { us += 1000000; --s; }
00134   return long(1000.0*s + us / 1000.0 + 0.5);
00135 
00136 #else
00137   // Win32 section
00138 # if defined(VCL_BORLAND)
00139   struct timeb real_time;
00140   ftime(&real_time);
00141 # else
00142   struct _timeb real_time;
00143   _ftime(&real_time);
00144 # endif
00145   s = long(real_time.time - data->real0.time);
00146   long ms = real_time.millitm - data->real0.millitm;
00147 
00148   if (ms < 0) { ms += 1000; --s; }
00149   return 1000*s + ms;
00150 #endif
00151 }
00152 
00153 
00154 long vul_timer::user()
00155 {
00156 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00157   tms usage;
00158   times(&usage);  // new user/system time
00159   return (usage.tms_utime - data->usage0.tms_utime) * 1000 / CLK_TCK;
00160 #else
00161   vcl_clock_t usage = vcl_clock();
00162   return (usage - data->usage0) / (CLOCKS_PER_SEC/1000);
00163 #endif
00164 }
00165 
00166 //: Returns the number of milliseconds spent in user-process or operating system respectively, since last mark().
00167 
00168 long vul_timer::system()
00169 {
00170 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00171   tms usage;
00172   times(&usage);  // new user/system time
00173   return (usage.tms_stime - data->usage0.tms_stime) * 1000 / CLK_TCK;
00174 #else
00175   return 0L;
00176 #endif
00177 }
00178 
00179 // Returns the number of milliseconds spent in user-process AND
00180 // operating system, since last mark().
00181 
00182 long vul_timer::all()
00183 {
00184 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00185   tms usage;
00186   times(&usage);  // new user/system time
00187   return (usage.tms_utime + usage.tms_stime -
00188           data->usage0.tms_utime - data->usage0.tms_stime)  * 1000 / CLK_TCK;
00189 #else
00190   vcl_clock_t usage = vcl_clock();
00191   return (usage - data->usage0) / (CLOCKS_PER_SEC/1000);
00192 #endif
00193 }
00194 
00195 //: Display user and real time since the last mark.
00196 void vul_timer::print(vcl_ostream& s)
00197 {
00198   s << "Time: user " << user() / 1000.0 << ", real " << this->real() / 1000.0 << vcl_endl;
00199 }