Go to the documentation of this file.00001
00002 #include "vul_timer.h"
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 #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;
00046 struct timeval real0;
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>
00058
00059 #include <vcl_climits.h>
00060 #include <vcl_iostream.h>
00061
00062
00063
00064
00065 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00066 #include <direct.h>
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
00086
00087 void vul_timer::mark()
00088 {
00089 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00090 times(&data->usage0);
00091 #ifndef SYSV
00092 struct timezone tz;
00093 gettimeofday(&data->real0, &tz);
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
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
00113
00114 long vul_timer::real()
00115 {
00116 long s;
00117
00118 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00119 struct timeval real_time;
00120 #ifndef SYSV
00121 struct timezone tz;
00122 gettimeofday(&real_time, &tz);
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
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);
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
00167
00168 long vul_timer::system()
00169 {
00170 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00171 tms usage;
00172 times(&usage);
00173 return (usage.tms_stime - data->usage0.tms_stime) * 1000 / CLK_TCK;
00174 #else
00175 return 0L;
00176 #endif
00177 }
00178
00179
00180
00181
00182 long vul_timer::all()
00183 {
00184 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00185 tms usage;
00186 times(&usage);
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
00196 void vul_timer::print(vcl_ostream& s)
00197 {
00198 s << "Time: user " << user() / 1000.0 << ", real " << this->real() / 1000.0 << vcl_endl;
00199 }