PSCF v1.1
Timer.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Timer.h"
9
10namespace Util
11{
12
13 // Static function
14
15 /*
16 * Return current time point (static function)
17 */
18 Timer::TimePoint Timer::now()
19 #ifdef UTIL_CXX11
20 { return Clock::now(); }
21 #else
22 { return clock(); }
23 #endif
24
25 // Non-static member functions
26
27 /*
28 * Constructor
29 */
31 : isRunning_(false)
32 { clear(); }
33
34 /*
35 * Get the current time.
36 */
37 double Timer::time()
38 {
39 #ifdef UTIL_CXX11
40 return time_.count();
41 #else
42 return time_;
43 #endif
44 }
45
46 /*
47 * Start the timer with externally supplied time point.
48 */
49 void Timer::start(TimePoint begin)
50 {
51 if (isRunning_) {
52 UTIL_THROW("Attempt to restart an active Timer");
53 }
54 isRunning_ = true;
55 begin_ = begin;
56 }
57
58 /*
59 * Start the timer.
60 */
62 {
63 if (isRunning_) {
64 UTIL_THROW("Attempt to restart an active Timer");
65 }
66 isRunning_ = true;
67 begin_ = Timer::now();
68 }
69
70 /*
71 * Stop the timer at externally supplied time point.
72 */
73 void Timer::stop(TimePoint end)
74 {
75 if (!isRunning_) {
76 UTIL_THROW("Attempt to stop an inactive Timer");
77 }
78 isRunning_ = false;
79 #ifdef UTIL_CXX11
80 time_ += end - begin_;
81 #else
82 time_ += double(end - begin_)/double(CLOCKS_PER_SEC);
83 #endif
84 }
85
86 /*
87 * Clear the timer.
88 */
90 {
91 #ifdef UTIL_CXX11
92 time_ = Duration::zero();
93 #else
94 time_ = 0.0;
95 #endif
96 isRunning_ = false;
97 }
98
99}
Timer()
Default constructor.
Definition: Timer.cpp:30
void stop()
Stop the clock now (internally supplied).
Definition: Timer.h:123
void start()
Start timing from now (internally computed).
Definition: Timer.cpp:61
void clear()
Reset accumulated time to zero.
Definition: Timer.cpp:89
static TimePoint now()
Return current time point.
Definition: Timer.cpp:18
double time()
Return the accumulated time, in seconds.
Definition: Timer.cpp:37
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1