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

Measures execution time between construction and destruction. AutoTimer can output either to a std::ostream or to a spdlog::logger More...

#include <lf/base/timer.h>

Public Member Functions

 AutoTimer (std::string format=Timer::kDefaultFormat)
 Create a new AutoTimer that will report time measurements to std::cout when destructed.
 
 AutoTimer (std::ostream &stream, std::string format=Timer::kDefaultFormat)
 Create a new AutoTimer that will report to the given stream during destruction.
 
 AutoTimer (std::shared_ptr< spdlog::logger > logger, spdlog::level::level_enum level=spdlog::level::level_enum::info, std::string format=Timer::kDefaultFormat)
 Create a new AutoTimer that will report to the given spdlog::logger on destruction.
 
 AutoTimer (const AutoTimer &)=default
 
 AutoTimer (AutoTimer &&)=default
 
AutoTimeroperator= (const AutoTimer &)=default
 
AutoTimeroperator= (AutoTimer &&)=default
 
 ~AutoTimer ()
 Destructor of AutoTimer, calls Report().
 
std::ostream & ostream () const
 Retrieve the output stream object that was passed in the constructor.
 
const std::shared_ptr< spdlog::logger > & logger () const
 Retrieve the logger that was passed in the constructor.
 
const std::string & FormatString () const
 Retrieve the format string that was passed in the constructor.
 
void Report ()
 Report time (wall, user, system) since construction of AutoTimer() to either a std::ostream or spdlog::logger that was passed in the constructor.
 
Timer::cpu_times Elapsed () const noexcept
 Retrieve the elapsed time since the construction.
 
std::string Format (std::string_view format=Timer::kDefaultFormat) const
 Return the number of elapsed seconds of the wall clock time, user time and system time as a formatted string.
 

Private Attributes

Timer timer_
 
std::string format_
 
std::variant< std::ostream *, std::pair< std::shared_ptr< spdlog::logger >, spdlog::level::level_enum > > output_
 

Detailed Description

Measures execution time between construction and destruction. AutoTimer can output either to a std::ostream or to a spdlog::logger

This class is very useful when you want to measure the execution time of a particular piece of code:

std::make_unique<lf::mesh::hybrid2d::MeshFactory>(2), "mesh.msh");
// We want to measure how long it takes to assemble a mass matrix on this
// mesh:
auto fes =
std::make_shared<lf::uscalfe::FeSpaceLagrangeO1<double>>(reader.mesh());
lf::assemble::COOMatrix<double> sm(fes->LocGlobMap().NumDofs(),
fes->LocGlobMap().NumDofs());
{
std::cout << "Start assembly" << std::endl;
// start measuring the runtime from here:
lf::assemble::AssembleMatrixLocally(0, fes->LocGlobMap(), fes->LocGlobMap(),
emp, sm);
} // upon destruction of the AutoTimer `at`, the measured runtime will be
// reported to std::cout.
See also
Timer

Definition at line 182 of file timer.h.

Constructor & Destructor Documentation

◆ AutoTimer() [1/5]

lf::base::AutoTimer::AutoTimer ( std::string format = Timer::kDefaultFormat)
explicit

Create a new AutoTimer that will report time measurements to std::cout when destructed.

Parameters
formatOptional format string that specifies how the measured execution time is printed. See Timer::Format().

Definition at line 142 of file timer.cc.

◆ AutoTimer() [2/5]

lf::base::AutoTimer::AutoTimer ( std::ostream & stream,
std::string format = Timer::kDefaultFormat )
explicit

Create a new AutoTimer that will report to the given stream during destruction.

Parameters
streamThe stream to which output should be written.
formatOptional format string that specifies how the measured execution time is printed. See Timer::Format().

Definition at line 145 of file timer.cc.

◆ AutoTimer() [3/5]

lf::base::AutoTimer::AutoTimer ( std::shared_ptr< spdlog::logger > logger,
spdlog::level::level_enum level = spdlog::level::level_enum::info,
std::string format = Timer::kDefaultFormat )
explicit

Create a new AutoTimer that will report to the given spdlog::logger on destruction.

Parameters
loggerThe spdlog logger object to which the timer should write. See Loggers and Debug output .
levelThe logging level at which the message will be logged.
formatOptional format string that specifies how the measured execution time is printed. See Timer::Format().

Definition at line 148 of file timer.cc.

◆ AutoTimer() [4/5]

lf::base::AutoTimer::AutoTimer ( const AutoTimer & )
default

◆ AutoTimer() [5/5]

lf::base::AutoTimer::AutoTimer ( AutoTimer && )
default

◆ ~AutoTimer()

lf::base::AutoTimer::~AutoTimer ( )

Destructor of AutoTimer, calls Report().

Definition at line 153 of file timer.cc.

References lf::base::Timer::IsStopped(), Report(), lf::base::Timer::Stop(), and timer_.

Member Function Documentation

◆ Elapsed()

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

Retrieve the elapsed time since the construction.

See also
Timer::Elapsed()

Definition at line 186 of file timer.cc.

References lf::base::Timer::Elapsed(), and timer_.

◆ Format()

std::string lf::base::AutoTimer::Format ( std::string_view format = Timer::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 190 of file timer.cc.

References lf::base::Timer::Format(), and timer_.

◆ FormatString()

const std::string & lf::base::AutoTimer::FormatString ( ) const

Retrieve the format string that was passed in the constructor.

Definition at line 175 of file timer.cc.

References format_.

◆ logger()

const std::shared_ptr< spdlog::logger > & lf::base::AutoTimer::logger ( ) const

Retrieve the logger that was passed in the constructor.

Exceptions
std::bad_variant_accessif a std::ostream was passed in the constructor.

Definition at line 169 of file timer.cc.

References output_.

◆ operator=() [1/2]

AutoTimer & lf::base::AutoTimer::operator= ( AutoTimer && )
default

◆ operator=() [2/2]

AutoTimer & lf::base::AutoTimer::operator= ( const AutoTimer & )
default

◆ ostream()

std::ostream & lf::base::AutoTimer::ostream ( ) const

Retrieve the output stream object that was passed in the constructor.

Exceptions
std::bad_variant_accessif a spdlog::logger was passed in the constructor.

Definition at line 165 of file timer.cc.

References output_.

Referenced by Report().

◆ Report()

void lf::base::AutoTimer::Report ( )

Report time (wall, user, system) since construction of AutoTimer() to either a std::ostream or spdlog::logger that was passed in the constructor.

Note
When the output is written to a std::ostream, this method will insert an additional newline character at the end.

Definition at line 177 of file timer.cc.

References lf::base::Timer::Format(), format_, ostream(), output_, and timer_.

Referenced by ~AutoTimer().

Member Data Documentation

◆ format_

std::string lf::base::AutoTimer::format_
private

Definition at line 268 of file timer.h.

Referenced by FormatString(), and Report().

◆ output_

std::variant<std::ostream*, std::pair<std::shared_ptr<spdlog::logger>, spdlog::level::level_enum> > lf::base::AutoTimer::output_
private

Definition at line 273 of file timer.h.

Referenced by logger(), ostream(), and Report().

◆ timer_

Timer lf::base::AutoTimer::timer_
private

Definition at line 267 of file timer.h.

Referenced by Elapsed(), Format(), Report(), and ~AutoTimer().


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