Horus Doc || C++ Reference || Class Overview   Pixels   Images   Detector   Geometry   Registry || Doxygen's quick Index  

HxTimer.h

00001 /*
00002  *  Copyright (c) 1998, University of Amsterdam, The Netherlands.
00003  *  All rights reserved.
00004  *
00005  *
00006  *  Author(s):
00007  *  Dennis Koelma (koelma@wins.uva.nl)
00008  *  Edo Poll (poll@wins.uva.nl)
00009  */
00010 
00011 #ifndef HxTimer_h
00012 #define HxTimer_h
00013 
00014 #ifdef unix
00015 #include <sys/param.h>
00016 #include <sys/times.h>
00017 #include <limits.h>
00018 #include <unistd.h>
00019 #else
00020 #include <ctime>
00021 #endif
00022 
00023 #include "HxStd.h"
00024 #include "HxIoFwd.h"
00025 
00026 class L_HXBASIS HxTimer {
00027 
00028 public:
00029 
00030                     HxTimer();
00031 
00032     void            start();
00033     void            stop();
00034     void            stop(STD_OSTREAM &);
00035 
00036     double          lastTime() const;
00037 
00038     STD_OSTREAM&    put(STD_OSTREAM &) const;
00039     STD_OSTREAM&    show(STD_OSTREAM &) const;
00040 
00041     HxTimer         operator-(const HxTimer &) const;
00042 
00043 private:
00044 
00045 #ifdef unix
00046     struct tms      cpuTime;
00047     time_t          sysTime;
00048     time_t          usrTime;
00049 #else
00050     clock_t         cpuTime;
00051 #endif
00052 };
00053 
00054 inline STD_OSTREAM&
00055 operator<<(STD_OSTREAM &os, const HxTimer &t)
00056 {
00057     return t.put(os);
00058 }
00059 
00060 inline
00061 HxTimer::HxTimer()
00062 {
00063     start();
00064 }
00065 
00066 inline void
00067 HxTimer::start()
00068 {
00069 #ifdef unix
00070     times(&cpuTime);
00071     usrTime = cpuTime.tms_utime;
00072     sysTime = cpuTime.tms_stime;
00073 #else
00074     cpuTime = clock();
00075 #endif
00076 }
00077 
00078 inline void
00079 HxTimer::stop()
00080 {
00081 #ifdef unix
00082     times(&cpuTime);
00083     usrTime = cpuTime.tms_utime - usrTime;
00084     sysTime = cpuTime.tms_stime - sysTime;
00085 #else
00086     cpuTime = clock() - cpuTime;
00087 #endif
00088 }
00089 
00090 inline double
00091 HxTimer::lastTime() const
00092 {
00093     double val;
00094 #ifdef unix
00095     val = (double) (usrTime+sysTime) / HZ;
00096 #else
00097     val = (double) cpuTime / CLOCKS_PER_SEC;
00098 #endif
00099     return val;
00100 }
00101 
00102 inline HxTimer
00103 HxTimer::operator-(const HxTimer &arg) const
00104 {
00105     HxTimer res(*this);
00106 
00107 #ifdef unix
00108     res.usrTime -= arg.usrTime;
00109     res.sysTime -= arg.sysTime;
00110 #else
00111     res.cpuTime -= arg.cpuTime;
00112 #endif
00113     
00114     return res;
00115 }
00116 
00117 #endif

Generated on Tue Feb 3 14:18:42 2004 for C++Reference by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001