1.1 --- a/lemon/time_measure.h Mon Apr 14 17:37:18 2008 +0100
1.2 +++ b/lemon/time_measure.h Mon Apr 14 10:46:41 2008 +0200
1.3 @@ -23,12 +23,16 @@
1.4 ///\file
1.5 ///\brief Tools for measuring cpu usage
1.6
1.7 +#ifdef WIN32
1.8 +#include <windows.h>
1.9 +#include <cmath>
1.10 +#else
1.11 #include <sys/times.h>
1.12 +#include <sys/time.h>
1.13 +#endif
1.14
1.15 -#include <sys/time.h>
1.16 #include <fstream>
1.17 #include <iostream>
1.18 -#include <unistd.h>
1.19
1.20 namespace lemon {
1.21
1.22 @@ -54,25 +58,14 @@
1.23
1.24 class TimeStamp
1.25 {
1.26 - struct rtms
1.27 - {
1.28 - double tms_utime;
1.29 - double tms_stime;
1.30 - double tms_cutime;
1.31 - double tms_cstime;
1.32 - rtms() {}
1.33 - rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
1.34 - tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
1.35 - };
1.36 - rtms ts;
1.37 - double real_time;
1.38 + double utime;
1.39 + double stime;
1.40 + double cutime;
1.41 + double cstime;
1.42 + double rtime;
1.43
1.44 - rtms &getTms() {return ts;}
1.45 - const rtms &getTms() const {return ts;}
1.46 -
1.47 void _reset() {
1.48 - ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0;
1.49 - real_time = 0;
1.50 + utime = stime = cutime = cstime = rtime = 0;
1.51 }
1.52
1.53 public:
1.54 @@ -80,11 +73,40 @@
1.55 ///Read the current time values of the process
1.56 void stamp()
1.57 {
1.58 +#ifndef WIN32
1.59 timeval tv;
1.60 - tms _ts;
1.61 - times(&_ts);
1.62 - gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
1.63 - ts=_ts;
1.64 + gettimeofday(&tv, 0);
1.65 + rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
1.66 +
1.67 + tms ts;
1.68 + double tck=sysconf(_SC_CLK_TCK);
1.69 + times(&ts);
1.70 + utime=ts.tms_utime/tck;
1.71 + stime=ts.tms_stime/tck;
1.72 + cutime=ts.tms_cutime/tck;
1.73 + cstime=ts.tms_cstime/tck;
1.74 +#else
1.75 + static const double ch = 4294967296.0e-7;
1.76 + static const double cl = 1.0e-7;
1.77 +
1.78 + FILETIME system;
1.79 + GetSystemTimeAsFileTime(&system);
1.80 + rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
1.81 +
1.82 + FILETIME create, exit, kernel, user;
1.83 + if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
1.84 + utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
1.85 + stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
1.86 + cutime = 0;
1.87 + cstime = 0;
1.88 + } else {
1.89 + rtime = 0;
1.90 + utime = 0;
1.91 + stime = 0;
1.92 + cutime = 0;
1.93 + cstime = 0;
1.94 + }
1.95 +#endif
1.96 }
1.97
1.98 /// Constructor initializing with zero
1.99 @@ -99,11 +121,11 @@
1.100 ///\e
1.101 TimeStamp &operator+=(const TimeStamp &b)
1.102 {
1.103 - ts.tms_utime+=b.ts.tms_utime;
1.104 - ts.tms_stime+=b.ts.tms_stime;
1.105 - ts.tms_cutime+=b.ts.tms_cutime;
1.106 - ts.tms_cstime+=b.ts.tms_cstime;
1.107 - real_time+=b.real_time;
1.108 + utime+=b.utime;
1.109 + stime+=b.stime;
1.110 + cutime+=b.cutime;
1.111 + cstime+=b.cstime;
1.112 + rtime+=b.rtime;
1.113 return *this;
1.114 }
1.115 ///\e
1.116 @@ -115,11 +137,11 @@
1.117 ///\e
1.118 TimeStamp &operator-=(const TimeStamp &b)
1.119 {
1.120 - ts.tms_utime-=b.ts.tms_utime;
1.121 - ts.tms_stime-=b.ts.tms_stime;
1.122 - ts.tms_cutime-=b.ts.tms_cutime;
1.123 - ts.tms_cstime-=b.ts.tms_cstime;
1.124 - real_time-=b.real_time;
1.125 + utime-=b.utime;
1.126 + stime-=b.stime;
1.127 + cutime-=b.cutime;
1.128 + cstime-=b.cstime;
1.129 + rtime-=b.rtime;
1.130 return *this;
1.131 }
1.132 ///\e
1.133 @@ -131,11 +153,11 @@
1.134 ///\e
1.135 TimeStamp &operator*=(double b)
1.136 {
1.137 - ts.tms_utime*=b;
1.138 - ts.tms_stime*=b;
1.139 - ts.tms_cutime*=b;
1.140 - ts.tms_cstime*=b;
1.141 - real_time*=b;
1.142 + utime*=b;
1.143 + stime*=b;
1.144 + cutime*=b;
1.145 + cstime*=b;
1.146 + rtime*=b;
1.147 return *this;
1.148 }
1.149 ///\e
1.150 @@ -148,11 +170,11 @@
1.151 ///\e
1.152 TimeStamp &operator/=(double b)
1.153 {
1.154 - ts.tms_utime/=b;
1.155 - ts.tms_stime/=b;
1.156 - ts.tms_cutime/=b;
1.157 - ts.tms_cstime/=b;
1.158 - real_time/=b;
1.159 + utime/=b;
1.160 + stime/=b;
1.161 + cutime/=b;
1.162 + cstime/=b;
1.163 + rtime/=b;
1.164 return *this;
1.165 }
1.166 ///\e
1.167 @@ -173,25 +195,31 @@
1.168 ///Gives back the user time of the process
1.169 double userTime() const
1.170 {
1.171 - return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
1.172 + return utime;
1.173 }
1.174 ///Gives back the system time of the process
1.175 double systemTime() const
1.176 {
1.177 - return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
1.178 + return stime;
1.179 }
1.180 ///Gives back the user time of the process' children
1.181 +
1.182 + ///\note On <tt>WIN32</tt> platform this value is not calculated.
1.183 + ///
1.184 double cUserTime() const
1.185 {
1.186 - return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
1.187 + return cutime;
1.188 }
1.189 ///Gives back the user time of the process' children
1.190 +
1.191 + ///\note On <tt>WIN32</tt> platform this value is not calculated.
1.192 + ///
1.193 double cSystemTime() const
1.194 {
1.195 - return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
1.196 + return cstime;
1.197 }
1.198 ///Gives back the real time
1.199 - double realTime() const {return real_time;}
1.200 + double realTime() const {return rtime;}
1.201 };
1.202
1.203 TimeStamp operator*(double b,const TimeStamp &t)
1.204 @@ -212,13 +240,14 @@
1.205 /// \li \c cs: system cpu time of children,
1.206 /// \li \c real: real time.
1.207 /// \relates TimeStamp
1.208 + /// \note On <tt>WIN32</tt> platform the cummulative values are not
1.209 + /// calculated.
1.210 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
1.211 {
1.212 - long cls = sysconf(_SC_CLK_TCK);
1.213 - os << "u: " << double(t.getTms().tms_utime)/cls <<
1.214 - "s, s: " << double(t.getTms().tms_stime)/cls <<
1.215 - "s, cu: " << double(t.getTms().tms_cutime)/cls <<
1.216 - "s, cs: " << double(t.getTms().tms_cstime)/cls <<
1.217 + os << "u: " << t.userTime() <<
1.218 + "s, s: " << t.systemTime() <<
1.219 + "s, cu: " << t.cUserTime() <<
1.220 + "s, cs: " << t.cSystemTime() <<
1.221 "s, real: " << t.realTime() << "s";
1.222 return os;
1.223 }
1.224 @@ -404,11 +433,17 @@
1.225 return operator TimeStamp().systemTime();
1.226 }
1.227 ///Gives back the ellapsed user time of the process' children
1.228 +
1.229 + ///\note On <tt>WIN32</tt> platform this value is not calculated.
1.230 + ///
1.231 double cUserTime() const
1.232 {
1.233 return operator TimeStamp().cUserTime();
1.234 }
1.235 ///Gives back the ellapsed user time of the process' children
1.236 +
1.237 + ///\note On <tt>WIN32</tt> platform this value is not calculated.
1.238 + ///
1.239 double cSystemTime() const
1.240 {
1.241 return operator TimeStamp().cSystemTime();