stpwatch.h
上传用户:kellyonhid
上传日期:2013-10-12
资源大小:932k
文件大小:2k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. // Template Numerical Toolkit (TNT) for Linear Algebra 
  2. //
  3. // R. Pozo
  4. // Applied and Computational Mathematics Division
  5. // National Institute of Standards and Technology
  6. #ifndef STPWATCH_H
  7. #define STPWATCH_H
  8. /*  Simple stopwatch object:
  9.         void    start()     : start timing
  10.         double  stop()      : stop timing
  11.         void    reset()     : set elapsed time to 0.0
  12.         double  read()      : read elapsed time (in seconds)
  13. */
  14. #include <time.h>
  15. inline double seconds(void)
  16. {
  17.     static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
  18.     return ( (double) clock() ) * secs_per_tick;
  19. }
  20. class stopwatch {
  21.     private:
  22.         int running;
  23.         double last_time;
  24.         double total;
  25.     public:
  26.         stopwatch() : running(0), last_time(0.0), total(0.0) {}
  27.         void reset() { running = 0; last_time = 0.0; total=0.0; }
  28.         void start() { if (!running) { last_time = seconds(); running = 1;}}
  29.         double stop()  { if (running) 
  30.                             {
  31.                                 total += seconds() - last_time; 
  32.                                 running = 0;
  33.                              }
  34.                           return total; 
  35.                         }
  36.         double read()   {  if (running) 
  37.                             {
  38.                                 total+= seconds() - last_time;
  39.                                 last_time = seconds();
  40.                             }
  41.                            return total;
  42.                         }       
  43.                             
  44. };
  45. #endif
  46.     
  47.