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 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 } /* TNT namespace */ 00094 #endif 00095 00096 00097