Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

misc/timer.cpp

Go to the documentation of this file.
00001 #include "timer.h"
00002 
00009 #include <iostream>
00010 using namespace std;
00011 
00012 #define THOUSAND 1000
00013 #define MILLION  1000000
00014 
00015 Timer::Timer()
00016 : _os(NULL)
00017 {
00018     start();
00019 }
00020 
00021 Timer::Timer(ostream &os)
00022 : _os(&os)
00023 {
00024     start();
00025 }
00026 
00027 Timer::~Timer()
00028 {
00029     if (_os)
00030         (*_os) << elapsed() << " sec." << endl;
00031 }
00032 
00033 double 
00034 Timer::elapsed() const
00035 {
00036     #ifdef GLT_WIN32
00037     return (double) (::clock()-_start)/CLOCKS_PER_SEC;
00038     #endif
00039 
00040     #ifdef GLT_UNIX
00041     return double(clock())/MILLION;
00042     #endif
00043 }
00044 
00045 clock_t 
00046 Timer::clock() const
00047 {
00048     #ifdef GLT_WIN32
00049     if (_frozenCount)
00050         return _frozen-_start;
00051     else
00052         return ::clock()-_start;
00053     #endif
00054 
00055     #ifdef GLT_UNIX
00056     
00057     /* TODO: after one hour or so, we get overflow.
00058              Perhaps we should migrate to milli-seconds for UNIX
00059              That extends our timeout to 1000 hours (or 41 days)  */
00060              
00061     if (_frozenCount)
00062         return (_frozen.tv_sec-_start.tv_sec)*MILLION+(_frozen.tv_usec-_start.tv_usec);
00063     else
00064     {
00065         timeval tmp;
00066         gettimeofday(&tmp,NULL);
00067         return (tmp.tv_sec-_start.tv_sec)*MILLION+(tmp.tv_usec-_start.tv_usec);
00068     }
00069     #endif
00070 }
00071 
00072 void 
00073 Timer::start()
00074 {
00075     #ifdef GLT_WIN32
00076         if (_frozenCount)
00077         _start = _end = _frozen;
00078     else
00079         _start = _end = ::clock();
00080     #endif
00081 
00082     #ifdef GLT_UNIX
00083     if (_frozenCount)
00084         _start = _end = _frozen;
00085     else
00086     {
00087         gettimeofday(&_start,NULL);
00088         _end = _start;
00089     }
00090     #endif
00091 }
00092 
00094 
00095 #ifdef GLT_WIN32
00096 clock_t Timer::_frozen = 0;
00097 #endif
00098 
00099 #ifdef GLT_UNIX
00100 timeval Timer::_frozen;
00101 #endif
00102 
00103 uint32  Timer::_frozenCount = 0;
00104 
00105 void 
00106 Timer::freeze()
00107 {
00108     #ifdef GLT_WIN32
00109     if (_frozenCount==0)
00110         _frozen = ::clock();
00111     #endif
00112 
00113     #ifdef GLT_UNIX
00114     if (_frozenCount==0)
00115         gettimeofday(&_frozen,NULL);
00116     #endif
00117 
00118     _frozenCount++;
00119 }
00120 
00121 void 
00122 Timer::unFreeze()
00123 {
00124     _frozenCount--;
00125 }
00126 
00127 void 
00128 Timer::advance(const uint32 msec)
00129 {
00130     #ifdef GLT_WIN32
00131     _frozen += (msec*CLOCKS_PER_SEC)/1000;
00132     #endif
00133 
00134     #ifdef GLT_UNIX
00135     _frozen.tv_sec  += (_frozen.tv_usec+(msec*THOUSAND))/MILLION;
00136     _frozen.tv_usec  = (_frozen.tv_usec+(msec*THOUSAND))%MILLION;
00137     #endif
00138 }
00139 

Generated on Tue Nov 5 11:11:05 2002 for GLT by doxygen1.2.18