Thrill  0.1
MultiTimer Class Reference

Detailed Description

MultiTimer can be used to measure time usage of different phases in a program or algorithm.

It contains multiple named "timers", which can be activated without prior definition. At most one timer is start at any time, which means start() will stop any current timer and start a new one.

Timers are identified by strings, which are passed as const char*, which MUST remain valid for the lifetime of the MultiTimer. Dynamic strings will not work, the standard way is to use plain string literals. The strings are hash for faster searches.

MultiTimer can also be used for multi-threading parallel programs. Each thread must create and keep its own MultiTimer instance, which can then be added together into a global MultiTimer object. The add() method of the global object is internally thread-safe using a global mutex.

Definition at line 36 of file multi_timer.hpp.

#include <multi_timer.hpp>

Public Member Functions

 MultiTimer ()
 constructor More...
 
 MultiTimer (const MultiTimer &)
 default copy-constructor More...
 
 MultiTimer (MultiTimer &&)
 move-constructor: default More...
 
 ~MultiTimer ()
 destructor More...
 
MultiTimeradd (const MultiTimer &b)
 
double get (const char *timer)
 return timer duration in seconds of timer. More...
 
MultiTimeroperator+= (const MultiTimer &b)
 
MultiTimeroperator= (const MultiTimer &)
 default assignment operator More...
 
MultiTimeroperator= (MultiTimer &&)
 move-assignment operator: default More...
 
void print (const char *info, std::ostream &os) const
 print all timers as a TIMER line to os More...
 
void print (const char *info) const
 print all timers as a TIMER line to stderr More...
 
void reset ()
 zero timers. More...
 
const char * running () const
 return name of currently running timer. More...
 
void start (const char *timer)
 start new timer phase, stop the currently running one. More...
 
void stop ()
 stop the currently running timer. More...
 
double total () const
 return total duration of all timers. More...
 

Private Member Functions

Entry & find_or_create (const char *name)
 internal methods to find or create new timer entries More...
 

Private Attributes

const char * running_
 currently running timer name More...
 
uint32_t running_hash_
 hash of running_ More...
 
std::chrono::time_point< std::chrono::high_resolution_clock > time_point_
 start of currently running timer name More...
 
std::vector< Entry > timers_
 array of timers More...
 
std::chrono::duration< double > total_duration_
 total duration More...
 

Constructor & Destructor Documentation

◆ MultiTimer() [1/3]

constructor

Definition at line 39 of file multi_timer.cpp.

References MultiTimer::operator=(), and MultiTimer::~MultiTimer().

◆ MultiTimer() [2/3]

MultiTimer ( const MultiTimer )
default

default copy-constructor

◆ MultiTimer() [3/3]

MultiTimer ( MultiTimer &&  )
default

move-constructor: default

◆ ~MultiTimer()

~MultiTimer ( )
default

destructor

Referenced by MultiTimer::MultiTimer().

Member Function Documentation

◆ add()

MultiTimer & add ( const MultiTimer b)

add all timers from another, internally holds a global mutex lock, because this is used to add thread values

Definition at line 127 of file multi_timer.cpp.

References MultiTimer::find_or_create(), MultiTimer::running_, MultiTimer::timers_, TLX_LOG1, and MultiTimer::total_duration_.

Referenced by MultiTimer::operator+=(), and ScopedMultiTimer::~ScopedMultiTimer().

◆ find_or_create()

MultiTimer::Entry & find_or_create ( const char *  name)
private

internal methods to find or create new timer entries

Definition at line 52 of file multi_timer.cpp.

References tlx::hash_djb2(), and MultiTimer::timers_.

Referenced by MultiTimer::add(), MultiTimer::get(), and MultiTimer::stop().

◆ get()

double get ( const char *  timer)

return timer duration in seconds of timer.

Definition at line 105 of file multi_timer.cpp.

References MultiTimer::find_or_create().

◆ operator+=()

MultiTimer & operator+= ( const MultiTimer b)

add all timers from another, internally holds a global mutex lock, because this is used to add thread values

Definition at line 140 of file multi_timer.cpp.

References MultiTimer::add().

◆ operator=() [1/2]

MultiTimer & operator= ( const MultiTimer )
default

default assignment operator

Referenced by MultiTimer::MultiTimer().

◆ operator=() [2/2]

MultiTimer & operator= ( MultiTimer &&  )
default

move-assignment operator: default

◆ print() [1/2]

void print ( const char *  info,
std::ostream &  os 
) const

print all timers as a TIMER line to os

Definition at line 113 of file multi_timer.cpp.

References MultiTimer::running_, MultiTimer::timers_, tlx_die_unless, and MultiTimer::total_duration_.

Referenced by MultiTimer::print().

◆ print() [2/2]

void print ( const char *  info) const

print all timers as a TIMER line to stderr

Definition at line 123 of file multi_timer.cpp.

References MultiTimer::print().

◆ reset()

void reset ( )

zero timers.

Definition at line 96 of file multi_timer.cpp.

References MultiTimer::timers_, and MultiTimer::total_duration_.

◆ running()

const char * running ( ) const

return name of currently running timer.

Definition at line 101 of file multi_timer.cpp.

References MultiTimer::running_.

Referenced by PS5SmallsortJob< Context, StringPtr, BktSizeType >::sort_mkqs_cache().

◆ start()

◆ stop()

◆ total()

double total ( ) const

return total duration of all timers.

Definition at line 109 of file multi_timer.cpp.

References MultiTimer::total_duration_.

Referenced by tlx::sort_strings_detail::parallel_sample_sort_base().

Member Data Documentation

◆ running_

const char* running_
private

currently running timer name

Definition at line 95 of file multi_timer.hpp.

Referenced by MultiTimer::add(), MultiTimer::print(), MultiTimer::running(), MultiTimer::start(), and MultiTimer::stop().

◆ running_hash_

uint32_t running_hash_
private

hash of running_

Definition at line 97 of file multi_timer.hpp.

Referenced by MultiTimer::start(), and MultiTimer::stop().

◆ time_point_

std::chrono::time_point<std::chrono::high_resolution_clock> time_point_
private

start of currently running timer name

Definition at line 99 of file multi_timer.hpp.

Referenced by MultiTimer::stop().

◆ timers_

std::vector<Entry> timers_
private

array of timers

Definition at line 86 of file multi_timer.hpp.

Referenced by MultiTimer::add(), MultiTimer::find_or_create(), MultiTimer::print(), and MultiTimer::reset().

◆ total_duration_

std::chrono::duration<double> total_duration_
private

total duration

Definition at line 92 of file multi_timer.hpp.

Referenced by MultiTimer::add(), MultiTimer::print(), MultiTimer::reset(), MultiTimer::stop(), and MultiTimer::total().


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