Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

time_measure.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * lemon/time_measure.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Research Group on Combinatorial Optimization, 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     }
00107     TimeStamp ellapsed() const
00108     {
00109       TimeStamp t(NULL);
00110       return t-*this;
00111     }
00112   
00113     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
00114   
00116     double getUserTime() const
00117     {
00118       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
00119     }
00121     double getSystemTime() const
00122     {
00123       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
00124     }
00126     double getCUserTime() const
00127     {
00128       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
00129     }
00131     double getCSystemTime() const
00132     {
00133       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
00134     }
00136     double getRealTime() const {return real_time;}
00137   };
00138 
00140 
00167   class Timer
00168   {
00169     TimeStamp start_time;
00170 
00171     void _reset() {start_time.stamp();}
00172   
00173   public: 
00175     Timer() {_reset();}
00176 
00178 
00182     operator TimeStamp () const
00183     {
00184       TimeStamp t;
00185       t.stamp();
00186       return t-start_time;
00187     }
00188 
00190 
00193     void reset()
00194     {
00195       _reset();
00196     }
00197 
00198 
00200     double getUserTime() const
00201     {
00202       return operator TimeStamp().getUserTime();
00203     }
00205     double getSystemTime() const
00206     {
00207       return operator TimeStamp().getSystemTime();
00208     }
00210     double getCUserTime() const
00211     {
00212       return operator TimeStamp().getCUserTime();
00213     }
00215     double getCSystemTime() const
00216     {
00217       return operator TimeStamp().getCSystemTime();
00218     }
00220     double getRealTime() const
00221     {
00222       return operator TimeStamp().getRealTime();
00223     }
00224 
00225   };
00226 
00228 
00240   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
00241   {
00242     long cls = sysconf(_SC_CLK_TCK);
00243     os << "u: " << double(t.getTms().tms_utime)/cls <<
00244       "s, s: " << double(t.getTms().tms_stime)/cls <<
00245       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
00246       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
00247       "s, real: " << t.getRealTime() << "s";
00248     return os;
00249   }
00250 
00252 
00253 } //namespace lemon
00254 
00255 #endif //LEMON_TIME_MEASURE_H

Generated on Sat Aug 27 14:14:55 2005 for LEMON by  doxygen 1.4.4