31 root.name = baseTimerName;
38 TimerNode* parent = current;
39 current = ¤t->childNodes[timerName];
41 current->name = timerName;
42 current->parent = parent;
55 current = current->parent;
58void Timer::TimerNode::tick() { timeKeeper.
start(); }
60void Timer::TimerNode::tock() { timeKeeper.stop(); }
62double Timer::lap(
const Key& timerName)
const {
return 0; }
66 const TimerPath& path, WallTimeDuration wallAdd, CpuTimeDuration cpuAdd,
int ticksAdd)
69 TimerNode& cursor = root;
70 for (
auto& nodeName : path) {
71 cursor = cursor.childNodes[nodeName];
81 TimerNode* cursor = current;
82 while (cursor != &root) {
83 path.push_front(cursor->name);
84 cursor = cursor->parent;
89Timer::TimerPath Timer::pathToFirstMatch(
const Key& timerName)
const
91 return root.searchDescendants(timerName);
94std::ostream&
Timer::report(
const Key& timerName, std::ostream& os)
const
96 return report(pathToFirstMatch(timerName), os);
99std::ostream&
Timer::report(std::ostream& os)
const {
return root.reportAll(os,
""); }
103 const TimerNode* cursor = &root;
104 for (
auto& element : path) {
105 cursor = &cursor->childNodes.at(element);
107 return cursor->report(os,
"");
112 root.childNodes.clear();
113 root.timeKeeper.
reset();
118Timer::TimerNode::TimerNode()
123Timer::TimerPath Timer::TimerNode::searchDescendants(
const Key& timerName)
const
126 for (
auto& children : childNodes) {
127 if (children.first == timerName) {
128 path.push_front(children.first);
129 }
else if (!children.second.searchDescendants(timerName).empty()) {
130 path.push_front(name);
136inline int msCountFromWall(
const Timer::WallTimeDuration& wall)
138 return std::chrono::duration_cast<std::chrono::microseconds>(wall).count();
141std::ostream& Timer::TimerNode::report(std::ostream& os,
const std::string& prefix)
const
145 WallTimeDuration wallTimeNow = timeKeeper.wallTime();
146 CpuTimeDuration cpuTimeNow = timeKeeper.cpuTime();
151 WallTimeDuration wallTimeParent = parent->timeKeeper.wallTime();
152 CpuTimeDuration cpuTimeParent = parent->timeKeeper.cpuTime();
153 pcParentWall = msCountFromWall(wallTimeNow) * 100. / msCountFromWall(wallTimeParent);
154 pcParentCpu = cpuTimeNow * 100. / cpuTimeParent;
160 double wallSeconds = msCountFromWall(wallTimeNow) * 1e-6;
162 os << name <<
": ticks = " << timeKeeper.ticks();
163 os <<
" wall time " << wallSeconds <<
" s"
164 <<
" (" << pcParentWall <<
"% of parent)";
165 os <<
" cpu time " << cpuTimeNow <<
" s"
166 <<
" (" << pcParentCpu <<
"% of parent)";
167 os <<
" " << timeKeeper.ticks() <<
" activations (" << 1e3 * wallSeconds / timeKeeper.ticks()
169 if (timeKeeper.running())
174static std::string branch =
"├";
175static std::string spc =
" ";
176static std::string cont =
"│";
177static std::string last =
"└";
179std::string extendPrefix(
const std::string& prefix)
181 std::string newPrefix = std::regex_replace(prefix, std::regex(branch), cont);
182 return std::regex_replace(newPrefix, std::regex(last), spc);
185std::ostream& Timer::TimerNode::reportAll(std::ostream& os,
const std::string& prefix)
const
190 int nNodes = childNodes.size();
193 for (
auto& child : childNodes) {
194 std::string lastBranch = (++iNode == nNodes) ? last : branch;
195 child.second.reportAll(os, extendPrefix(prefix) + lastBranch);
void extraWallTime(const WallTimeDuration &extraTime)
Adds an externally determined increment to the wall clock.
void reset()
Resets all of the chronometer counters.
void extraCpuTime(const CpuTimeDuration &extraTime)
Adds an externally determined increment to the CPU clock.
void extraTicks(int extraTicks)
Adds an externally determined increment to the activation count.
void start()
Starts the timer.
A class for a hierarchical timer functions.
void additionalTime(const TimerPath &path, WallTimeDuration additionalWall, CpuTimeDuration additionalCpu, int additionalTicks)
Adds an additional time increment to a timer.
double elapsed(const Key &timerName) const
Returns the elapsed time.
void tock()
Stop the last timer to be started.
std::ostream & report(const Key &timerName, std::ostream &os) const
Prints the status of a named timer to an ostream.
void tick(const Key &timerName)
Starts a named timer.
Timer()
Creates a Timer with an unnamed root node.
double lap(const Key &timerName) const
Returns the elapsed time without stopping the timer.
void reset()
Deletes all timers except the root, which is reset.
TimerPath currentTimerNodePath() const
Returns the timer path to the currently running timer.
static Timer main
Static timer for general use.