code_profiler
profiler.cpp
Go to the documentation of this file.
2 
3 #include <iostream>
4 
5 namespace tue
6 {
7 
8 // ----------------------------------------------------------------------------------------------------
9 
10 Profiler::Profiler() : head_(nullptr), parent_(nullptr)
11 {
12 }
13 
14 // ----------------------------------------------------------------------------------------------------
15 
16 Profiler::Profiler(const std::string& name) : name_(name), head_(nullptr), parent_(nullptr)
17 {
18 }
19 
20 // ----------------------------------------------------------------------------------------------------
21 
22 Profiler::Profiler(const std::string& name, Profiler* parent) : name_(name), head_(nullptr), parent_(parent)
23 {
24 }
25 
26 // ----------------------------------------------------------------------------------------------------
27 
29 {
30  for(std::map<std::string, Profiler*>::iterator it = children_.begin(); it != children_.end(); ++it)
31  {
32  delete it->second;
33  }
34 }
35 
36 // ----------------------------------------------------------------------------------------------------
37 
39 {
40  if (!head_)
41  {
42  head_ = this;
43  }
44 
45  Profiler* child;
47  if (it_child != head_->children_.end())
48  {
49  child = it_child->second;
50  }
51  else
52  {
53  child = new Profiler(name, head_);
54  head_->children_[name] = child;
55  }
56 
57  head_ = child;
58 
59  child->timer_.start();
60 }
61 
62 // ----------------------------------------------------------------------------------------------------
63 
65 {
66  if (!head_)
67  {
68  std::cout << "[tue::Profiler] stopTimer() called, but no timer is active." << std::endl;
69  return;
70  }
71 
72  head_->timer_.stop();
73  head_ = head_->parent_;
74 }
75 
76 // ----------------------------------------------------------------------------------------------------
77 
78 void Profiler::addToStream(std::ostream& out, const std::string& prefix) const
79 {
80  if (!parent_)
81  {
82  out << prefix << "[" << name_ << "]" << std::endl;
83  }
84  else
85  {
86  out << prefix << name_ << ": " << timer_.getElapsedTimeInMilliSec() << " ms" << std::endl;
87  }
88 
89  for(std::map<std::string, Profiler*>::const_iterator it = children_.begin(); it != children_.end(); ++it)
90  {
91  it->second->addToStream(out, prefix + " ");
92  }
93 }
94 
95 // ----------------------------------------------------------------------------------------------------
96 
98 {
99  p.addToStream(out);
100  return out;
101 }
102 
103 }
tue::operator<<
std::ostream & operator<<(std::ostream &out, const Profiler &p)
Definition: profiler.cpp:97
std::string
tue::Profiler::timer_
Timer timer_
Definition: profiler.h:40
tue::Profiler::head_
Profiler * head_
Definition: profiler.h:36
tue::Profiler::stopTimer
void stopTimer()
Definition: profiler.cpp:64
tue::Profiler::Profiler
Profiler()
Definition: profiler.cpp:10
tue::Profiler::startTimer
void startTimer(const std::string &name)
Definition: profiler.cpp:38
iostream
tue::Timer::stop
void stop()
Definition: timer.cpp:61
tue::Timer::start
void start()
Definition: timer.cpp:51
std::cout
profiler.h
tue::Profiler::parent_
Profiler * parent_
Definition: profiler.h:38
tue::Profiler::name_
std::string name_
Definition: profiler.h:34
tue::Timer::getElapsedTimeInMilliSec
long double getElapsedTimeInMilliSec() const
Get elasped time in milli-seconds.
Definition: timer.cpp:97
std::ostream
tue::Profiler::addToStream
void addToStream(std::ostream &out, const std::string &prefix="") const
Definition: profiler.cpp:78
std::map
tue::Profiler
Definition: profiler.h:11
std::endl
T endl(T... args)
tue::Profiler::children_
std::map< std::string, Profiler * > children_
Definition: profiler.h:42
tue::Profiler::~Profiler
virtual ~Profiler()
Definition: profiler.cpp:28
tue
Definition: loop_timer.h:8