Essentia  2.1-beta5-dev
tnt_stopwatch.h
Go to the documentation of this file.
1 /*
2 *
3 * Mathematical and Computational Sciences Division
4 * National Institute of Technology,
5 * Gaithersburg, MD USA
6 *
7 *
8 * This software was developed at the National Institute of Standards and
9 * Technology (NIST) by employees of the Federal Government in the course
10 * of their official duties. Pursuant to title 17 Section 105 of the
11 * United States Code, this software is not subject to copyright protection
12 * and is in the public domain. NIST assumes no responsibility whatsoever for
13 * its use by other parties, and makes no guarantees, expressed or implied,
14 * about its quality, reliability, or any other characteristic.
15 *
16 */
17 
18 
19 
20 #ifndef STOPWATCH_H
21 #define STOPWATCH_H
22 
23 // for clock() and CLOCKS_PER_SEC
24 #include <time.h>
25 
26 
27 namespace TNT
28 {
29 
30 inline static double seconds(void)
31 {
32  const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
33  return ( (double) clock() ) * secs_per_tick;
34 }
35 
36 class Stopwatch {
37  private:
38  int running_;
39  double start_time_;
40  double total_;
41 
42  public:
43  inline Stopwatch();
44  inline void start();
45  inline double stop();
46  inline double read();
47  inline void resume();
48  inline int running();
49 };
50 
51 inline Stopwatch::Stopwatch() : running_(0), start_time_(0.0), total_(0.0) {}
52 
54 {
55  running_ = 1;
56  total_ = 0.0;
57  start_time_ = seconds();
58 }
59 
61 {
62  if (running_)
63  {
64  total_ += (seconds() - start_time_);
65  running_ = 0;
66  }
67  return total_;
68 }
69 
70 inline void Stopwatch::resume()
71 {
72  if (!running_)
73  {
74  start_time_ = seconds();
75  running_ = 1;
76  }
77 }
78 
79 
80 inline double Stopwatch::read()
81 {
82  if (running_)
83  {
84  stop();
85  resume();
86  }
87  return total_;
88 }
89 
90 
91 } /* TNT namespace */
92 #endif
93 
94 
95 
Stopwatch()
Definition: tnt_stopwatch.h:51
Definition: tnt_stopwatch.h:36
void resume()
Definition: tnt_stopwatch.h:70
Definition: tnt_array1d.h:35
double stop()
Definition: tnt_stopwatch.h:60
double read()
Definition: tnt_stopwatch.h:80
double total_
Definition: tnt_stopwatch.h:40
double start_time_
Definition: tnt_stopwatch.h:39
static double seconds(void)
Definition: tnt_stopwatch.h:30
int running_
Definition: tnt_stopwatch.h:38
void start()
Definition: tnt_stopwatch.h:53