00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef STOPWATCH_H
00021 #define STOPWATCH_H
00022
00023
00024 #include <time.h>
00025
00026
00027 namespace TNT
00028 {
00029
00030 inline static double seconds(void)
00031 {
00032 const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
00033 return ( (double) clock() ) * secs_per_tick;
00034 }
00035
00036
00037
00038 class Stopwatch {
00039 private:
00040 int running_;
00041 double start_time_;
00042 double total_;
00043
00044 public:
00045 inline Stopwatch();
00046 inline void start();
00047 inline double stop();
00048 inline double read();
00049 inline void resume();
00050 inline int running();
00051 };
00052
00053 inline Stopwatch::Stopwatch() : running_(0), start_time_(0.0), total_(0.0) {}
00054
00055 void Stopwatch::start()
00056 {
00057 running_ = 1;
00058 total_ = 0.0;
00059 start_time_ = seconds();
00060 }
00061
00062 double Stopwatch::stop()
00063 {
00064 if (running_)
00065 {
00066 total_ += (seconds() - start_time_);
00067 running_ = 0;
00068 }
00069 return total_;
00070 }
00071
00072 inline void Stopwatch::resume()
00073 {
00074 if (!running_)
00075 {
00076 start_time_ = seconds();
00077 running_ = 1;
00078 }
00079 }
00080
00081
00082 inline double Stopwatch::read()
00083 {
00084 if (running_)
00085 {
00086 stop();
00087 resume();
00088 }
00089 return total_;
00090 }
00091
00092
00093 }
00094 #endif
00095
00096
00097