Thrill
0.1
|
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... | |
MultiTimer & | add (const MultiTimer &b) |
double | get (const char *timer) |
return timer duration in seconds of timer. More... | |
MultiTimer & | operator+= (const MultiTimer &b) |
MultiTimer & | operator= (const MultiTimer &) |
default assignment operator More... | |
MultiTimer & | operator= (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... | |
MultiTimer | ( | ) |
constructor
Definition at line 39 of file multi_timer.cpp.
References MultiTimer::operator=(), and MultiTimer::~MultiTimer().
|
default |
default copy-constructor
|
default |
move-constructor: default
|
default |
destructor
Referenced by MultiTimer::MultiTimer().
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().
|
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().
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().
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().
|
default |
default assignment operator
Referenced by MultiTimer::MultiTimer().
|
default |
move-assignment operator: default
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().
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().
void reset | ( | ) |
zero timers.
Definition at line 96 of file multi_timer.cpp.
References MultiTimer::timers_, and MultiTimer::total_duration_.
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().
void start | ( | const char * | timer | ) |
start new timer phase, stop the currently running one.
Definition at line 66 of file multi_timer.cpp.
References tlx::hash_djb2(), MultiTimer::running_, MultiTimer::running_hash_, MultiTimer::stop(), tlx_die_unless, and TLX_LOG1.
Referenced by tlx::sort_strings_detail::parallel_sample_sort_base(), PS5SmallsortJob< Context, StringPtr, BktSizeType >::run(), ScopedMultiTimer::ScopedMultiTimer(), ScopedMultiTimerSwitch::ScopedMultiTimerSwitch(), and ScopedMultiTimerSwitch::~ScopedMultiTimerSwitch().
void stop | ( | ) |
stop the currently running timer.
Definition at line 84 of file multi_timer.cpp.
References MultiTimer::find_or_create(), MultiTimer::running_, MultiTimer::running_hash_, MultiTimer::time_point_, and MultiTimer::total_duration_.
Referenced by tlx::sort_strings_detail::parallel_sample_sort_base(), MultiTimer::start(), PS5SmallsortJob< Context, StringPtr, BktSizeType >::~PS5SmallsortJob(), and ScopedMultiTimer::~ScopedMultiTimer().
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().
|
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().
|
private |
hash of running_
Definition at line 97 of file multi_timer.hpp.
Referenced by MultiTimer::start(), and MultiTimer::stop().
|
private |
start of currently running timer name
Definition at line 99 of file multi_timer.hpp.
Referenced by MultiTimer::stop().
|
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().
|
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().