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