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