delpi
0.0.1
DElta-complete LP solver
Toggle main menu visibility
Loading...
Searching...
No Matches
Timer.cpp
1
6
7
#include "delpi/util/Timer.h"
8
9
#include <sys/resource.h>
10
11
#include <stdexcept>
12
13
#include "delpi/util/exception.h"
14
#include "delpi/util/logging.h"
15
16
namespace
delpi
{
17
18
template
<
class
T>
19
TimerBase<T>::TimerBase() : last_start_{now()} {}
20
21
template
<
class
T>
22
void
TimerBase<T>::Start
() {
23
DELPI_TRACE(
"TimerBase::Start"
);
24
last_start_
= now();
25
elapsed_
= duration{0};
26
running_
=
true
;
27
}
28
29
template
<
class
T>
30
void
TimerBase<T>::Pause
() {
31
if
(
running_
) {
32
running_
=
false
;
33
elapsed_
+= (now() -
last_start_
);
34
}
35
}
36
37
template
<
class
T>
38
void
TimerBase<T>::Resume
() {
39
if
(!
running_
) {
40
last_start_
= now();
41
running_
=
true
;
42
}
43
}
44
45
template
<
class
T>
46
bool
TimerBase<T>::is_running()
const
{
47
return
running_
;
48
}
49
50
template
<
class
T>
51
typename
TimerBase<T>::duration TimerBase<T>::elapsed()
const
{
52
DELPI_TRACE(
"TimerBase::duration"
);
53
return
running_ ? elapsed_ + (now() - last_start_) : elapsed_;
54
}
55
56
template
<
class
T>
57
std::chrono::duration<double>::rep TimerBase<T>::seconds()
const
{
58
DELPI_TRACE(
"TimerBase::seconds"
);
59
return
std::chrono::duration_cast<std::chrono::duration<double>>(elapsed()).count();
60
}
61
62
user_clock::time_point user_clock::now() {
63
DELPI_TRACE(
"user_clock::now"
);
64
struct
rusage usage{};
65
if
(0 != getrusage(RUSAGE_SELF, &usage))
throw
DelpiException(
"Failed to get current resource usage (getrusage)"
);
66
return
time_point(duration(
static_cast<
uint64_t
>
(usage.ru_utime.tv_sec) * std::micro::den +
67
static_cast<
uint64_t
>
(usage.ru_utime.tv_usec)));
68
}
69
70
// Explicit instantiations
71
template
class
TimerBase<chosen_steady_clock>;
72
template
class
TimerBase<user_clock>;
73
74
TimerGuard::TimerGuard
(
Timer
*
const
timer,
const
bool
enabled,
const
bool
start_timer)
75
:
timer_
{timer},
enabled_
{enabled &&
timer_
!= nullptr} {
76
if
(
enabled_
&& start_timer)
timer_
->Resume();
77
}
78
79
TimerGuard::~TimerGuard
() {
80
if
(
enabled_
)
timer_
->Pause();
81
}
82
83
void
TimerGuard::Pause
() {
84
if
(
enabled_
)
timer_
->Pause();
85
}
86
87
void
TimerGuard::Resume
() {
88
if
(
enabled_
)
timer_
->Resume();
89
}
90
91
}
// namespace delpi
delpi::TimerBase::Resume
void Resume()
Resume the timer.
Definition
Timer.cpp:38
delpi::TimerBase::elapsed_
duration elapsed_
Elapsed time so far. This doesn't include the current fragment if it is running.
Definition
Timer.h:73
delpi::TimerBase::Pause
void Pause()
Pause the timer.
Definition
Timer.cpp:30
delpi::TimerBase::Start
void Start()
Start the timer.
Definition
Timer.cpp:22
delpi::TimerBase::last_start_
time_point last_start_
Last time_point when the timer is started or resumed.
Definition
Timer.h:72
delpi::TimerBase::running_
bool running_
Whether the timer is running or not.
Definition
Timer.h:71
delpi::TimerGuard::Pause
void Pause()
Pause the guarded timer object.
Definition
Timer.cpp:83
delpi::TimerGuard::enabled_
const bool enabled_
Whether the timer is enabled.
Definition
Timer.h:165
delpi::TimerGuard::TimerGuard
TimerGuard(Timer *timer, bool enabled, bool start_timer=true)
Construct a new TimeGuard object.
Definition
Timer.cpp:74
delpi::TimerGuard::Resume
void Resume()
Resume the guarded timer object.
Definition
Timer.cpp:87
delpi::TimerGuard::timer_
Timer *const timer_
The timer to be guarded.
Definition
Timer.h:164
delpi::TimerGuard::~TimerGuard
~TimerGuard()
When the timer guard object is destructed, it pauses the embedded timer object.
Definition
Timer.cpp:79
delpi::Timer
Timer class using the a steady clock.
Definition
Timer.h:95
delpi
Global namespace for the delpi library.
delpi
util
Timer.cpp
Generated by
1.17.0