tlx
Loading...
Searching...
No Matches
multi_timer.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/multi_timer.hpp
3 *
4 * Part of tlx - http://panthema.net/tlx
5 *
6 * Copyright (C) 2018-2019 Timo Bingmann <tb@panthema.net>
7 *
8 * All rights reserved. Published under the Boost Software License, Version 1.0
9 ******************************************************************************/
10
11#ifndef TLX_MULTI_TIMER_HEADER
12#define TLX_MULTI_TIMER_HEADER
13
14#include <chrono>
15#include <cstdint>
16#include <ostream>
17#include <vector>
18
19namespace tlx {
20
21/*!
22 * MultiTimer can be used to measure time usage of different phases in a program
23 * or algorithm. It contains multiple named "timers", which can be activated
24 * without prior definition. At most one timer is start at any time, which
25 * means `start()` will stop any current timer and start a new one.
26 *
27 * Timers are identified by strings, which are passed as const char*, which MUST
28 * remain valid for the lifetime of the MultiTimer. Dynamic strings will not
29 * work, the standard way is to use plain string literals. The strings are hash
30 * for faster searches.
31 *
32 * MultiTimer can also be used for multi-threading parallel programs. Each
33 * thread must create and keep its own MultiTimer instance, which can then be
34 * added together into a global MultiTimer object. The add() method of the
35 * global object is internally thread-safe using a global mutex.
36 */
38{
39public:
40 //! constructor
41 MultiTimer();
42
43 //! default copy-constructor
45 //! default assignment operator
47 //! move-constructor: default
49 //! move-assignment operator: default
51
52 //! destructor
54
55 //! start new timer phase, stop the currently running one.
56 void start(const char* timer);
57
58 //! stop the currently running timer.
59 void stop();
60
61 //! zero timers.
62 void reset();
63
64 //! return name of currently running timer.
65 const char * running() const;
66
67 //! return timer duration in seconds of timer.
68 double get(const char* timer);
69 //! return total duration of all timers.
70 double total() const;
71
72 //! print all timers as a TIMER line to os
73 void print(const char* info, std::ostream& os) const;
74 //! print all timers as a TIMER line to stderr
75 void print(const char* info) const;
76
77 //! add all timers from another, internally holds a global mutex lock,
78 //! because this is used to add thread values
79 MultiTimer& add(const MultiTimer& b);
80
81 //! add all timers from another, internally holds a global mutex lock,
82 //! because this is used to add thread values
84
85private:
86 //! timer entry
87 struct Entry;
88
89 //! array of timers
90 std::vector<Entry> timers_;
91
92 //! total duration
93 std::chrono::duration<double> total_duration_;
94
95 //! currently running timer name
96 const char* running_;
97 //! hash of running_
98 std::uint32_t running_hash_;
99 //! start of currently running timer name
100 std::chrono::time_point<std::chrono::high_resolution_clock> time_point_;
101
102 //! internal methods to find or create new timer entries
103 Entry& find_or_create(const char* name);
104};
105
106//! RAII Scoped MultiTimer switcher: switches the timer of a MultiTimer on
107//! construction and back to old one on destruction.
109{
110public:
111 //! construct and timer to switch to
112 ScopedMultiTimerSwitch(MultiTimer& timer, const char* new_timer);
113
114 //! change back timer to previous timer.
116
117protected:
118 //! reference to MultiTimer
120
121 //! previous timer, used to switch back to on destruction
122 const char* previous_;
123};
124
125//! Independent RAII Scoped MultiTimer: contains a MultiTimer which is started
126//! with the given timer, and added to the base MultiTimer on destruction.
128{
129public:
130 //! construct and change timer to tm
131 ScopedMultiTimer(MultiTimer& base, const char* timer);
132
133 //! change back timer to previous timer.
135
136protected:
137 //! reference to base timer
139
140 //! contained independent timer
142};
143
144} // namespace tlx
145
146#endif // !TLX_MULTI_TIMER_HEADER
147
148/******************************************************************************/
MultiTimer can be used to measure time usage of different phases in a program or algorithm.
Definition: multi_timer.hpp:38
void print(const char *info, std::ostream &os) const
print all timers as a TIMER line to os
void start(const char *timer)
start new timer phase, stop the currently running one.
Definition: multi_timer.cpp:65
MultiTimer()
constructor
Definition: multi_timer.cpp:39
const char * running_
currently running timer name
Definition: multi_timer.hpp:96
Entry & find_or_create(const char *name)
internal methods to find or create new timer entries
Definition: multi_timer.cpp:51
MultiTimer(MultiTimer &&)
move-constructor: default
std::vector< Entry > timers_
array of timers
Definition: multi_timer.hpp:90
MultiTimer & operator+=(const MultiTimer &b)
add all timers from another, internally holds a global mutex lock, because this is used to add thread...
~MultiTimer()
destructor
MultiTimer(const MultiTimer &)
default copy-constructor
std::chrono::duration< double > total_duration_
total duration
Definition: multi_timer.hpp:93
std::uint32_t running_hash_
hash of running_
Definition: multi_timer.hpp:98
void stop()
stop the currently running timer.
Definition: multi_timer.cpp:83
const char * running() const
return name of currently running timer.
MultiTimer & add(const MultiTimer &b)
add all timers from another, internally holds a global mutex lock, because this is used to add thread...
MultiTimer & operator=(const MultiTimer &)
default assignment operator
double get(const char *timer)
return timer duration in seconds of timer.
std::chrono::time_point< std::chrono::high_resolution_clock > time_point_
start of currently running timer name
void reset()
zero timers.
Definition: multi_timer.cpp:95
double total() const
return total duration of all timers.
RAII Scoped MultiTimer switcher: switches the timer of a MultiTimer on construction and back to old o...
~ScopedMultiTimerSwitch()
change back timer to previous timer.
MultiTimer & timer_
reference to MultiTimer
const char * previous_
previous timer, used to switch back to on destruction
Independent RAII Scoped MultiTimer: contains a MultiTimer which is started with the given timer,...
MultiTimer & base_
reference to base timer
~ScopedMultiTimer()
change back timer to previous timer.
MultiTimer timer_
contained independent timer