Last change
on this file since 2ab812d was
d85c194,
checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago
|
Remaining modules refactored
|
-
Property mode set to
100644
|
File size:
1.7 KB
|
Line | |
---|
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 | |
---|
53 | void Stopwatch::start() |
---|
54 | { |
---|
55 | running_ = 1; |
---|
56 | total_ = 0.0; |
---|
57 | start_time_ = seconds(); |
---|
58 | } |
---|
59 | |
---|
60 | double Stopwatch::stop() |
---|
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 | |
---|
Note: See
TracBrowser
for help on using the repository browser.