LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Attributes | Private Attributes | List of all members
lf::base::Timer Class Reference

Timer class to measure time. More...

#include <lf/base/timer.h>

Classes

struct  cpu_times
 Packages the elapsed wall clock time, user process CPU time, and system process CPU time. More...
 

Public Member Functions

 Timer (bool start=true) noexcept
 Construct a new timer and optionally start it immediately.
 
bool IsStopped () const noexcept
 Is the timer currently stopped?
 
cpu_times Elapsed () const noexcept
 Elapsed time since timer start, doesn't stop the timer.
 
std::string Format (std::string_view format=kDefaultFormat) const
 Return the number of elapsed seconds of the wall clock time, user time and system time as a formatted string.
 
void Start () noexcept
 (Re)starts the timer, i.e. the timer starts counting from 0 when this method is called.
 
void Stop () noexcept
 stop the timer, stop counting the elapsed time.
 
void Resume () noexcept
 Resume the timer, i.e. start counting from where we stopped the last time.
 

Static Public Attributes

static std::string kDefaultFormat
 Default format string that is used by Format()
 

Private Attributes

cpu_times times_
 
bool is_stopped_
 

Detailed Description

Timer class to measure time.

Note
This class is very similar to the boost::cpu_timer class.
See also
AutoTimer
boost::cpu_timer

Definition at line 64 of file timer.h.

Constructor & Destructor Documentation

◆ Timer()

lf::base::Timer::Timer ( bool start = true)
inlineexplicitnoexcept

Construct a new timer and optionally start it immediately.

Parameters
startIf set to true, the timer will start immediately. Otherwise it will stay in status stopped.

Definition at line 101 of file timer.h.

References is_stopped_, Start(), and times_.

Member Function Documentation

◆ Elapsed()

Timer::cpu_times lf::base::Timer::Elapsed ( ) const
noexcept

Elapsed time since timer start, doesn't stop the timer.

Definition at line 86 of file timer.cc.

References IsStopped(), lf::base::Timer::cpu_times::system, times_, lf::base::Timer::cpu_times::user, and lf::base::Timer::cpu_times::wall.

Referenced by lf::base::AutoTimer::Elapsed().

◆ Format()

std::string lf::base::Timer::Format ( std::string_view format = kDefaultFormat) const

Return the number of elapsed seconds of the wall clock time, user time and system time as a formatted string.

Parameters
formatThe fmt format string that is used to format the output. See below for possible fmt arguments.
Returns
The formatted string

format named arguments:

The following named arguments can appear in the format parameter:

name replacement value
w Elapsed().wall
u Elapsed().user
s Elapsed().system
t Elapsed().user + Elapsed().system
p (Elapsed().user + Elapsed().system)/Elapsed().wall*100.

Definition at line 100 of file timer.cc.

References lf::base::Timer::cpu_times::system, times_, lf::base::Timer::cpu_times::user, and lf::base::Timer::cpu_times::wall.

Referenced by lf::base::AutoTimer::Format(), and lf::base::AutoTimer::Report().

◆ IsStopped()

bool lf::base::Timer::IsStopped ( ) const
inlinenoexcept

Is the timer currently stopped?

Definition at line 115 of file timer.h.

References is_stopped_.

Referenced by Elapsed(), Resume(), Stop(), and lf::base::AutoTimer::~AutoTimer().

◆ Resume()

void lf::base::Timer::Resume ( )
noexcept

Resume the timer, i.e. start counting from where we stopped the last time.

Definition at line 132 of file timer.cc.

References IsStopped(), Start(), lf::base::Timer::cpu_times::system, times_, lf::base::Timer::cpu_times::user, and lf::base::Timer::cpu_times::wall.

◆ Start()

void lf::base::Timer::Start ( )
noexcept

(Re)starts the timer, i.e. the timer starts counting from 0 when this method is called.

Definition at line 114 of file timer.cc.

References is_stopped_, and times_.

Referenced by Resume(), and Timer().

◆ Stop()

void lf::base::Timer::Stop ( )
noexcept

stop the timer, stop counting the elapsed time.

Definition at line 119 of file timer.cc.

References is_stopped_, IsStopped(), lf::base::Timer::cpu_times::system, times_, lf::base::Timer::cpu_times::user, and lf::base::Timer::cpu_times::wall.

Referenced by lf::base::AutoTimer::~AutoTimer().

Member Data Documentation

◆ is_stopped_

bool lf::base::Timer::is_stopped_
private

Definition at line 169 of file timer.h.

Referenced by IsStopped(), Start(), Stop(), and Timer().

◆ kDefaultFormat

std::string lf::base::Timer::kDefaultFormat
inlinestatic
Initial value:
=
"{w:.6}s wall, {u:.6}s user + {s:.6}s system = {t:.6}s CPU ({p:.3}%)"

Default format string that is used by Format()

Definition at line 164 of file timer.h.

◆ times_

cpu_times lf::base::Timer::times_
private

Definition at line 168 of file timer.h.

Referenced by Elapsed(), Format(), Resume(), Start(), Stop(), and Timer().


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