Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vul_timer.cxx

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

Generated on Thu Jan 10 14:41:00 2008 for core/vul by  doxygen 1.4.4