00001 /* 00002 * 00003 * Mathematical and Computational Sciences Division 00004 * National Institute of Technology, 00005 * Gaithersburg, MD USA 00006 * 00007 * 00008 * This software was developed at the National Institute of Standards and 00009 * Technology (NIST) by employees of the Federal Government in the course 00010 * of their official duties. Pursuant to title 17 Section 105 of the 00011 * United States Code, this software is not subject to copyright protection 00012 * and is in the public domain. NIST assumes no responsibility whatsoever for 00013 * its use by other parties, and makes no guarantees, expressed or implied, 00014 * about its quality, reliability, or any other characteristic. 00015 * 00016 */ 00017 00018 00019 00020 #ifndef STOPWATCH_H 00021 #define STOPWATCH_H 00022 00023 // for clock() and CLOCKS_PER_SEC 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 class Stopwatch { 00037 private: 00038 int running_; 00039 double start_time_; 00040 double total_; 00041 00042 public: 00043 inline Stopwatch(); 00044 inline void start(); 00045 inline double stop(); 00046 inline double read(); 00047 inline void resume(); 00048 inline int running(); 00049 }; 00050 00051 inline Stopwatch::Stopwatch() : running_(0), start_time_(0.0), total_(0.0) {} 00052 00053 void Stopwatch::start() 00054 { 00055 running_ = 1; 00056 total_ = 0.0; 00057 start_time_ = seconds(); 00058 } 00059 00060 double Stopwatch::stop() 00061 { 00062 if (running_) 00063 { 00064 total_ += (seconds() - start_time_); 00065 running_ = 0; 00066 } 00067 return total_; 00068 } 00069 00070 inline void Stopwatch::resume() 00071 { 00072 if (!running_) 00073 { 00074 start_time_ = seconds(); 00075 running_ = 1; 00076 } 00077 } 00078 00079 00080 inline double Stopwatch::read() 00081 { 00082 if (running_) 00083 { 00084 stop(); 00085 resume(); 00086 } 00087 return total_; 00088 } 00089 00090 00091 } /* TNT namespace */ 00092 #endif 00093 00094 00095