Blender  V2.59
stopwatch.h
Go to the documentation of this file.
00001 
00005 /*
00006 
00007 *
00008 * Template Numerical Toolkit (TNT): Linear Algebra Module
00009 *
00010 * Mathematical and Computational Sciences Division
00011 * National Institute of Technology,
00012 * Gaithersburg, MD USA
00013 *
00014 *
00015 * This software was developed at the National Institute of Standards and
00016 * Technology (NIST) by employees of the Federal Government in the course
00017 * of their official duties. Pursuant to title 17 Section 105 of the
00018 * United States Code, this software is not subject to copyright protection
00019 * and is in the public domain.  The Template Numerical Toolkit (TNT) is
00020 * an experimental system.  NIST assumes no responsibility whatsoever for
00021 * its use by other parties, and makes no guarantees, expressed or implied,
00022 * about its quality, reliability, or any other characteristic.
00023 *
00024 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00025 * see http://math.nist.gov/tnt for latest updates.
00026 *
00027 */
00028 
00029 #ifndef STPWATCH_H
00030 #define STPWATCH_H
00031 
00032 // for clock() and CLOCKS_PER_SEC
00033 #include <ctime>
00034 
00035 namespace TNT
00036 {
00037 
00038 /*  Simple stopwatch object:
00039 
00040         void    start()     : start timing
00041         double  stop()      : stop timing
00042         void    reset()     : set elapsed time to 0.0
00043         double  read()      : read elapsed time (in seconds)
00044 
00045 */
00046 
00047 inline double seconds(void)
00048 {
00049     static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
00050     return ( (double) clock() ) * secs_per_tick;
00051 }
00052 
00053 
00054 class stopwatch {
00055     private:
00056         int running;
00057         double last_time;
00058         double total;
00059 
00060     public:
00061         stopwatch() : running(0), last_time(0.0), total(0.0) {}
00062         void reset() { running = 0; last_time = 0.0; total=0.0; }
00063         void start() { if (!running) { last_time = seconds(); running = 1;}}
00064         double stop()  { if (running) 
00065                             {
00066                                 total += seconds() - last_time; 
00067                                 running = 0;
00068                              }
00069                           return total; 
00070                         }
00071         double read()   {  if (running) 
00072                             {
00073                                 total+= seconds() - last_time;
00074                                 last_time = seconds();
00075                             }
00076                            return total;
00077                         }       
00078                             
00079 };
00080 
00081 } // namespace TNT
00082 
00083 #endif
00084