My Project
Loading...
Searching...
No Matches
tnt_stopwatch.h
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
36
37
38class Stopwatch {
39 private:
40 int running_;
41 double start_time_;
42 double total_;
43
44 public:
45 inline Stopwatch();
46 inline void start();
47 inline double stop();
48 inline double read();
49 inline void resume();
50 inline int running();
51};
52
53inline Stopwatch::Stopwatch() : running_(0), start_time_(0.0), total_(0.0) {}
54
55void Stopwatch::start()
56{
57 running_ = 1;
58 total_ = 0.0;
59 start_time_ = seconds();
60}
61
62double Stopwatch::stop()
63{
64 if (running_)
65 {
66 total_ += (seconds() - start_time_);
67 running_ = 0;
68 }
69 return total_;
70}
71
72inline void Stopwatch::resume()
73{
74 if (!running_)
75 {
76 start_time_ = seconds();
77 running_ = 1;
78 }
79}
80
81
82inline double Stopwatch::read()
83{
84 if (running_)
85 {
86 stop();
87 resume();
88 }
89 return total_;
90}
91
92
93} /* TNT namespace */
94#endif
95
96
97
Definition tnt_stopwatch.h:38
Definition tnt_array1d.h:36