00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _TIMERH_
00014 #define _TIMERH_
00015
00016 #include <ctime>
00017 #include "common_headers.hpp"
00018 #include "String.hpp"
00019
00020 class Timer {
00021 public:
00022 Timer(const String &string, int start=1) : message(string)
00023 { if (start) Start(); }
00024 void Start()
00025 {
00026 time(&t);
00027 cerr << "Beginning: " << message << endl << flush;
00028 }
00029 ~Timer() { if (t > 0) Stop(); }
00030 void Stop()
00031 {
00032 time_t total;
00033 time(&total);
00034 total -= t;
00035 cerr << message << " total time: ";
00036 if (total/3600) { cerr << total/3600 << " hours "; total %= 3600; }
00037 if (total/60) { cerr << total/60 << " minutes "; total %= 60; }
00038 cerr << total << " seconds " << endl << flush;
00039 t = 0;
00040 }
00041 void Elapsed()
00042 {
00043 time_t total;
00044 time(&total);
00045 total -= t;
00046 cerr << message << " elapsed time: ";
00047 if (total/3600) { cerr << total/3600 << " hours "; total %= 3600; }
00048 if (total/60) { cerr << total/60 << " minutes "; total %= 60; }
00049 cerr << total << " seconds " << endl << flush;
00050 }
00051 time_t getElapsed() const { time_t cur; time(&cur); return cur - t; }
00052 private:
00053 time_t t;
00054 String message;
00055 };
00056
00057
00058
00059 #endif