delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
delpi::TimerGuard Class Reference

The TimeGuard wraps a timer object and pauses it when the guard object is destructed. More...

#include <Timer.h>

Public Member Functions

 TimerGuard (Timer *timer, bool enabled, bool start_timer=true)
 Construct a new TimeGuard object.
 ~TimerGuard ()
 When the timer guard object is destructed, it pauses the embedded timer object.
void Pause ()
 Pause the guarded timer object.
void Resume ()
 Resume the guarded timer object.

Private Attributes

Timer *const timer_
 The timer to be guarded.
const bool enabled_ {false}
 Whether the timer is enabled.

Detailed Description

The TimeGuard wraps a timer object and pauses it when the guard object is destructed.

Useful for measuring the exact time spent in a block of code.

// Example usage
class MyClass {
private:
public:
void function_to_measure() {
TimerGuard guard(&timer_, true);
// Code to measure
}
// Return the total time elapsed, even across multiple calls
double time_elapsed() { return timer_.seconds(); }
};
TimerGuard(Timer *timer, bool enabled, bool start_timer=true)
Construct a new TimeGuard object.
Definition Timer.cpp:74
Timer *const timer_
The timer to be guarded.
Definition Timer.h:164
Timer class using the a steady clock.
Definition Timer.h:95

Definition at line 132 of file Timer.h.

Constructor & Destructor Documentation

◆ TimerGuard()

delpi::TimerGuard::TimerGuard ( Timer * timer,
bool enabled,
bool start_timer = true )

Construct a new TimeGuard object.

If enabled is false or timer is a nullptr, this class does not do anything. If start_timer is true, starts the timer in the constructor. Otherwise, it does not start it and a user has to call Resume() to start it.

Parameters
timera pointer to the timer object to be guarded
enabledwhether the timer is enabled and should run
start_timerwhether the timer should be started as soon as the guard is created

Definition at line 74 of file Timer.cpp.

◆ ~TimerGuard()

delpi::TimerGuard::~TimerGuard ( )

When the timer guard object is destructed, it pauses the embedded timer object.

If the timer is not enabled, this function does nothing.

Definition at line 79 of file Timer.cpp.


The documentation for this class was generated from the following files: