00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __TIMER_H
00035 #define __TIMER_H
00036
00037 #include <time.h>
00038 #include <stdlib.h>
00039 #include <sys/types.h>
00040 #include <sys/timeb.h>
00041
00042 #include <iostream>
00043 using std::cerr;
00044 using std::endl;
00045
00046
00047 #include "OFELI_Config.h"
00048
00049 namespace OFELI {
00050
00061 class Timer
00062 {
00063
00064 public:
00065
00067 Timer()
00068 {
00069 _ct = 0;
00070 _st0 = _st1 = false;
00071 #ifdef _OFELI_DEBUG
00072 std::clog << "An instance of class Timer is constructed.\n";
00073 std::clog << "File : " << __FILE__ << ", Line : " << __LINE__ << endl;
00074 #endif
00075 }
00076
00078 ~Timer() {
00079 #ifdef _OFELI_DEBUG
00080 std::clog << "An instance of class Timer is destructed.\n";
00081 std::clog << "File : " << __FILE__ << ", Line : " << __LINE__ << endl;
00082 #endif
00083 }
00084
00086 void Start()
00087 {
00088 _t0 = clock();
00089 _st0 = true;
00090 }
00091
00093 void Stop()
00094 {
00095 if (!_st0) {
00096 cerr << "\n*** Fatal Error in OFELI ***\nIn Timer::Stop()";
00097 cerr << " : Method Start() must have been called before." << endl;
00098 exit(1);
00099 }
00100 _t1 = clock();
00101 _ct += _t1-_t0;
00102 _st1 = true;
00103 }
00104
00106 void Resume()
00107 {
00108 if (!_st0) {
00109 cerr << "\n*** Fatal Error in OFELI ***\nIn Timer::Resume()";
00110 cerr << " : Method Start() must have been called before.\n";
00111 exit(1);
00112 }
00113 if (!_st1) {
00114 cerr << "\n*** Fatal Error in OFELI ***\nIn Timer::Resume()";
00115 cerr << " Method Stop() must have been called before.\n";
00116 cerr << "Call method 'Start()'\n";
00117 exit(1);
00118 }
00119 _t1 = clock();
00120 }
00121
00123 void Clear()
00124 {
00125 _t0 = clock();
00126 _ct = 0;
00127 }
00128
00130 double getTime()
00131 {
00132 if (!_st0) {
00133 cerr << "\n*** Fatal Error in OFELI ***\nIn Timer::getTime()";
00134 cerr << " : Method Start() must have been called before.\n";
00135 exit(1);
00136 }
00137 if (!_st1) {
00138 cerr << "\n*** Fatal Error in OFELI ***\nIn Timer::getTime()";
00139 cerr << " Method Stop() must have been called before.\n";
00140 exit(1);
00141 }
00142 return _ct/double(CLOCKS_PER_SEC);
00143 }
00144
00145 private:
00146 clock_t _t0, _t1, _ct;
00147 bool _st0, _st1;
00148 };
00149
00150 }
00151
00152 #endif