PLaSK library
Loading...
Searching...
No Matches
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
27namespace TNT
28{
29
30inline 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
36class 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
51inline 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
70inline void Stopwatch::resume()
71{
72 if (!running_)
73 {
74 start_time_ = seconds();
75 running_ = 1;
76 }
77}
78
79
80inline 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