dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Timer.cpp
1
7#include "Timer.h"
8
9#include <sys/resource.h>
10
11#include <stdexcept>
12
13#include "dlinear/util/logging.h"
14
15namespace dlinear {
16
17template <class T>
18TimerBase<T>::TimerBase() : last_start_{now()} {}
19
20template <class T>
22 DLINEAR_TRACE("TimerBase::Start");
23 last_start_ = now();
24 elapsed_ = duration{0};
25 running_ = true;
26}
27
28template <class T>
30 if (running_) {
31 running_ = false;
32 elapsed_ += (now() - last_start_);
33 }
34}
35
36template <class T>
38 if (!running_) {
39 last_start_ = now();
40 running_ = true;
41 }
42}
43
44template <class T>
46 return running_;
48
49template <class T>
50typename TimerBase<T>::duration TimerBase<T>::elapsed() const {
51 DLINEAR_TRACE("TimerBase::duration");
52 return running_ ? elapsed_ + (now() - last_start_) : elapsed_;
53}
55template <class T>
56std::chrono::duration<double>::rep TimerBase<T>::seconds() const {
57 DLINEAR_TRACE("TimerBase::seconds");
58 return std::chrono::duration_cast<std::chrono::duration<double>>(elapsed()).count();
60
61user_clock::time_point user_clock::now() {
62 DLINEAR_TRACE("user_clock::now");
63 struct rusage usage{};
64 if (0 != getrusage(RUSAGE_SELF, &usage)) throw std::runtime_error("Failed to get current resource usage (getrusage)");
65 return time_point(duration(uint64_t(usage.ru_utime.tv_sec) * std::micro::den + uint64_t(usage.ru_utime.tv_usec)));
66}
67
68// Explicit instantiations
70template class TimerBase<user_clock>;
71
72TimerGuard::TimerGuard(Timer *const timer, const bool enabled, const bool start_timer)
73 : timer_{timer}, enabled_{enabled && timer_ != nullptr} {
74 if (enabled_ && start_timer) timer_->Resume();
75}
76
80
82 if (enabled_) timer_->Pause();
83}
84
86 if (enabled_) timer_->Resume();
87}
88
89} // namespace dlinear
Simple timer class to evaluate the performance of the software.
Definition Timer.h:26
TimerBase()
Construct a new TimerBase object.
Definition Timer.cpp:18
duration elapsed() const
Get read-only access to the duration of elapsed time of the timer.
Definition Timer.cpp:50
bool is_running() const
Check whether the timer is running.
Definition Timer.cpp:45
void Resume()
Resume the timer.
Definition Timer.cpp:37
void Start()
Start the timer.
Definition Timer.cpp:21
void Pause()
Pause the timer.
Definition Timer.cpp:29
std::chrono::duration< double >::rep seconds() const
Get read-only access to the number elapsed seconds of the timer.
Definition Timer.cpp:56
const bool enabled_
Whether the timer is enabled.
Definition Timer.h:162
~TimerGuard()
When the timer guard object is destructed, it pauses the embedded timer object.
Definition Timer.cpp:77
void Resume()
Resume the guarded timer object.
Definition Timer.cpp:85
Timer *const timer_
The timer to be guarded.
Definition Timer.h:161
TimerGuard(Timer *timer, bool enabled, bool start_timer=true)
Construct a new TimeGuard object.
Definition Timer.cpp:72
void Pause()
Pause the guarded timer object.
Definition Timer.cpp:81
Timer class using the a steady clock.
Definition Timer.h:95
Global namespace for the dlinear library.