Blender V2.61 - r43446

stopwatch.h

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