Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
stopwatch.h
1 /*
2 This file is distributed as part of the BIAS library (Basic ImageAlgorithmS)
3 but it has not been developed by the authors of BIAS.
4 
5 For copyright, author and license information see below.
6 */
7 
8 
9 /*
10 *
11 * Template Numerical Toolkit (TNT): Linear Algebra Module
12 *
13 * Mathematical and Computational Sciences Division
14 * National Institute of Technology,
15 * Gaithersburg, MD USA
16 *
17 *
18 * This software was developed at the National Institute of Standards and
19 * Technology (NIST) by employees of the Federal Government in the course
20 * of their official duties. Pursuant to title 17 Section 105 of the
21 * United States Code, this software is not subject to copyright protection
22 * and is in the public domain. The Template Numerical Toolkit (TNT) is
23 * an experimental system. NIST assumes no responsibility whatsoever for
24 * its use by other parties, and makes no guarantees, expressed or implied,
25 * about its quality, reliability, or any other characteristic.
26 *
27 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
28 * see http://math.nist.gov/tnt for latest updates.
29 *
30 */
31 
32 
33 
34 #ifndef STPWATCH_H
35 #define STPWATCH_H
36 
37 // for clock() and CLOCKS_PER_SEC
38 #include <ctime>
39 
40 namespace TNT
41 {
42 
43  /* Simple stopwatch object:
44 
45  void start() : start timing
46  double stop() : stop timing
47  void reset() : set elapsed time to 0.0
48  double read() : read elapsed time (in seconds)
49 
50  */
51 
52  inline double seconds(void)
53  {
54  static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
55  return ( (double) clock() ) * secs_per_tick;
56  }
57 
58 
59  class stopwatch {
60  protected:
61  int running;
62  double last_time;
63  double total;
64 
65  public:
66  stopwatch() : running(0), last_time(0.0), total(0.0) {}
67  void reset() { running = 0; last_time = 0.0; total=0.0; }
68  void start() { if (!running) { last_time = seconds(); running = 1;}}
69  double stop() { if (running)
70  {
71  total += seconds() - last_time;
72  running = 0;
73  }
74  return total;
75  }
76  double read() { if (running)
77  {
78  total+= seconds() - last_time;
79  last_time = seconds();
80  }
81  return total;
82  }
83 
84  };
85 
86 } // namespace TNT
87 
88 #endif
89 
90 
91 
void reset()
Definition: stopwatch.h:67
double total
Definition: stopwatch.h:63
double seconds(void)
Definition: stopwatch.h:52
double last_time
Definition: stopwatch.h:62
double stop()
Definition: stopwatch.h:69
void start()
Definition: stopwatch.h:68
double read()
Definition: stopwatch.h:76