time_measure.h

Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library 00003 * 00004 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 00005 * (Egervary Combinatorial Optimization Research Group, EGRES). 00006 * 00007 * Permission to use, modify and distribute this software is granted 00008 * provided that this copyright notice appears in all copies. For 00009 * precise terms see the accompanying LICENSE file. 00010 * 00011 * This software is provided "AS IS" with no warranty of any kind, 00012 * express or implied, and with no claim as to its suitability for any 00013 * purpose. 00014 * 00015 */ 00016 00017 #ifndef LEMON_TIME_MEASURE_H 00018 #define LEMON_TIME_MEASURE_H 00019 00023 00024 #include <sys/time.h> 00025 #include <sys/times.h> 00026 #include <fstream> 00027 #include <iostream> 00028 #include <unistd.h> 00029 00030 namespace lemon { 00031 00034 00036 00050 00051 class TimeStamp 00052 { 00053 tms ts; 00054 double real_time; 00055 00056 public: 00057 00058 tms &getTms() {return ts;} 00059 const tms &getTms() const {return ts;} 00061 void stamp() 00062 { 00063 timeval tv; 00064 times(&ts); 00065 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; 00066 } 00067 00069 TimeStamp() 00070 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} 00072 TimeStamp(void *) { stamp();} 00073 00075 TimeStamp &operator+=(const TimeStamp &b) 00076 { 00077 ts.tms_utime+=b.ts.tms_utime; 00078 ts.tms_stime+=b.ts.tms_stime; 00079 ts.tms_cutime+=b.ts.tms_cutime; 00080 ts.tms_cstime+=b.ts.tms_cstime; 00081 real_time+=b.real_time; 00082 return *this; 00083 } 00085 TimeStamp operator+(const TimeStamp &b) const 00086 { 00087 TimeStamp t(*this); 00088 return t+=b; 00089 } 00091 TimeStamp &operator-=(const TimeStamp &b) 00092 { 00093 ts.tms_utime-=b.ts.tms_utime; 00094 ts.tms_stime-=b.ts.tms_stime; 00095 ts.tms_cutime-=b.ts.tms_cutime; 00096 ts.tms_cstime-=b.ts.tms_cstime; 00097 real_time-=b.real_time; 00098 return *this; 00099 } 00101 TimeStamp operator-(const TimeStamp &b) const 00102 { 00103 TimeStamp t(*this); 00104 return t-=b; 00105 } 00106 00108 TimeStamp ellapsed() const 00109 { 00110 TimeStamp t(NULL); 00111 return t-*this; 00112 } 00113 00114 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); 00115 00117 double getUserTime() const 00118 { 00119 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK); 00120 } 00122 double getSystemTime() const 00123 { 00124 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK); 00125 } 00127 double getCUserTime() const 00128 { 00129 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK); 00130 } 00132 double getCSystemTime() const 00133 { 00134 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); 00135 } 00137 double getRealTime() const {return real_time;} 00138 }; 00139 00141 00168 class Timer 00169 { 00170 TimeStamp start_time; 00171 00172 void _reset() {start_time.stamp();} 00173 00174 public: 00176 Timer() {_reset();} 00177 00179 00183 operator TimeStamp () 00184 { 00185 TimeStamp t; 00186 t.stamp(); 00187 return t-start_time; 00188 } 00189 00191 TimeStamp reset() 00192 { 00193 TimeStamp t(start_time); 00194 _reset(); 00195 return start_time-t; 00196 } 00197 }; 00198 00200 00212 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) 00213 { 00214 long cls = sysconf(_SC_CLK_TCK); 00215 os << "u: " << double(t.getTms().tms_utime)/cls << 00216 "s, s: " << double(t.getTms().tms_stime)/cls << 00217 "s, cu: " << double(t.getTms().tms_cutime)/cls << 00218 "s, cs: " << double(t.getTms().tms_cstime)/cls << 00219 "s, real: " << t.getRealTime() << "s"; 00220 return os; 00221 } 00222 00224 00225 } //namespace lemon 00226 00227 #endif //LEMON_TIME_MEASURE_H

Generated on Thu Sep 30 12:18:34 2004 for LEMON by doxygen 1.3.8