KiCad PCB EDA Suite
PROF_TIMER Class Reference

A small class to help profiling. More...

#include <profile.h>

Inheritance diagram for PROF_TIMER:
SCOPED_PROF_TIMER< DURATION >

Public Member Functions

 PROF_TIMER (const std::string &aName, bool aAutostart=true)
 Create a PROF_COUNTER for measuring an elapsed time in milliseconds. More...
 
 PROF_TIMER ()
 Create a PROF_COUNTER for measuring an elapsed time in milliseconds. More...
 
void Start ()
 Start or restart the counter. More...
 
void Stop ()
 Save the time when this function was called, and set the counter stane to stop. More...
 
void Show (std::ostream &aStream=std::cerr)
 Print the elapsed time (in a suitable unit) to a stream. More...
 
template<typename DURATION >
DURATION SinceStart (bool aSinceLast=false)
 
double msecs (bool aSinceLast=false)
 
std::string to_string ()
 

Private Types

using CLOCK = std::chrono::high_resolution_clock
 
using TIME_POINT = std::chrono::time_point< CLOCK >
 

Private Attributes

std::string m_name
 
bool m_running
 
TIME_POINT m_starttime
 
TIME_POINT m_lasttime
 
TIME_POINT m_stoptime
 

Detailed Description

A small class to help profiling.

It allows the calculation of the elapsed time (in milliseconds) between its creation (or the last call to Start() ) and the last call to Stop()

Definition at line 46 of file profile.h.

Member Typedef Documentation

◆ CLOCK

using PROF_TIMER::CLOCK = std::chrono::high_resolution_clock
private

Definition at line 164 of file profile.h.

◆ TIME_POINT

using PROF_TIMER::TIME_POINT = std::chrono::time_point<CLOCK>
private

Definition at line 165 of file profile.h.

Constructor & Destructor Documentation

◆ PROF_TIMER() [1/2]

PROF_TIMER::PROF_TIMER ( const std::string &  aName,
bool  aAutostart = true 
)
inline

Create a PROF_COUNTER for measuring an elapsed time in milliseconds.

Parameters
aNamea string that will be printed in message.
aAutostarttrue (default) to immediately start the timer

Definition at line 55 of file profile.h.

55  :
56  m_name( aName ), m_running( false )
57  {
58  if( aAutostart )
59  Start();
60  }
bool m_running
Definition: profile.h:162
std::string m_name
Definition: profile.h:161
void Start()
Start or restart the counter.
Definition: profile.h:75

References Start().

◆ PROF_TIMER() [2/2]

PROF_TIMER::PROF_TIMER ( )
inline

Create a PROF_COUNTER for measuring an elapsed time in milliseconds.

The counter is started and the string to print in message is left empty.

Definition at line 67 of file profile.h.

68  {
69  Start();
70  }
void Start()
Start or restart the counter.
Definition: profile.h:75

References Start().

Member Function Documentation

◆ msecs()

double PROF_TIMER::msecs ( bool  aSinceLast = false)
inline
Parameters
aSinceLastonly get the time since the last time the time was read.
Returns
the elapsed time in ms since the timer was started.

Definition at line 147 of file profile.h.

148  {
149  using DUR_MS = std::chrono::duration<double, std::milli>;
150  return SinceStart<DUR_MS>( aSinceLast ).count();
151  }

Referenced by QA_SEXPR_PARSER::Parse(), CONNECTION_GRAPH::Recalculate(), and to_string().

◆ Show()

void PROF_TIMER::Show ( std::ostream &  aStream = std::cerr)
inline

Print the elapsed time (in a suitable unit) to a stream.

The unit is automatically chosen from ns, us, ms and s, depending on the size of the current count.

Parameters
thestream to print to.

Definition at line 103 of file profile.h.

104  {
105  using DURATION = std::chrono::duration<double, std::nano>;
106 
107  const auto duration = SinceStart<DURATION>();
108  const double cnt = duration.count();
109 
110  if( m_name.size() )
111  {
112  aStream << m_name << " took ";
113  }
114 
115  if( cnt < 1e3 )
116  aStream << cnt << "ns";
117  else if( cnt < 1e6 )
118  aStream << cnt / 1e3 << "┬Ás";
119  else if( cnt < 1e9 )
120  aStream << cnt / 1e6 << "ms";
121  else
122  aStream << cnt / 1e9 << "s";
123 
124  aStream << std::endl;
125  }
std::string m_name
Definition: profile.h:161

References m_name.

Referenced by CONNECTION_GRAPH::buildConnectionGraph(), polygon_triangulation_main(), and CONNECTION_GRAPH::Recalculate().

◆ SinceStart()

template<typename DURATION >
DURATION PROF_TIMER::SinceStart ( bool  aSinceLast = false)
inline
Returns
the time since the timer was started. If the timer is stopped, the duration is from the start time to the time it was stopped, else it is to the current time.

Definition at line 133 of file profile.h.

134  {
135  const TIME_POINT stoptime = m_running ? CLOCK::now() : m_stoptime;
136  const TIME_POINT starttime = aSinceLast ? m_lasttime : m_starttime;
137 
138  m_lasttime = stoptime;
139 
140  return std::chrono::duration_cast<DURATION>( stoptime - starttime );
141  }
std::chrono::time_point< CLOCK > TIME_POINT
Definition: profile.h:165
TIME_POINT m_stoptime
Definition: profile.h:167
bool m_running
Definition: profile.h:162
TIME_POINT m_starttime
Definition: profile.h:167
TIME_POINT m_lasttime
Definition: profile.h:167

References m_lasttime, m_running, m_starttime, and m_stoptime.

Referenced by parse(), and SCOPED_PROF_TIMER< DURATION >::~SCOPED_PROF_TIMER().

◆ Start()

void PROF_TIMER::Start ( )
inline

Start or restart the counter.

Definition at line 75 of file profile.h.

76  {
77  m_running = true;
78  m_starttime = CLOCK::now();
80  }
bool m_running
Definition: profile.h:162
TIME_POINT m_starttime
Definition: profile.h:167
TIME_POINT m_lasttime
Definition: profile.h:167

References m_lasttime, m_running, and m_starttime.

Referenced by EDA_DRAW_PANEL_GAL::DoRePaint(), KIGFX::OPENGL_GAL::EndDrawing(), and PROF_TIMER().

◆ Stop()

void PROF_TIMER::Stop ( )
inline

Save the time when this function was called, and set the counter stane to stop.

Definition at line 86 of file profile.h.

87  {
88  if( !m_running )
89  return;
90 
91  m_stoptime = CLOCK::now();
92  m_running = false;
93  }
TIME_POINT m_stoptime
Definition: profile.h:167
bool m_running
Definition: profile.h:162

References m_running, and m_stoptime.

Referenced by EDA_DRAW_PANEL_GAL::DoRePaint(), KIGFX::GPU_CACHED_MANAGER::EndDrawing(), KIGFX::OPENGL_GAL::EndDrawing(), CONNECTION_GRAPH::Recalculate(), render_perftest_main_func(), and PCB_TEST_FRAME_BASE::SetBoard().

◆ to_string()

std::string PROF_TIMER::to_string ( )
inline

Definition at line 153 of file profile.h.

154  {
155  char tmp[1024];
156  snprintf( tmp, sizeof( tmp ), "%s: %-6.1fms", m_name.c_str(), msecs() );
157  return tmp;
158  }
double msecs(bool aSinceLast=false)
Definition: profile.h:147
std::string m_name
Definition: profile.h:161

References m_name, and msecs().

Referenced by EDA_DRAW_PANEL_GAL::DoRePaint(), KIGFX::GPU_CACHED_MANAGER::EndDrawing(), KIGFX::OPENGL_GAL::EndDrawing(), render_perftest_main_func(), and PCB_TEST_FRAME_BASE::SetBoard().

Member Data Documentation

◆ m_lasttime

TIME_POINT PROF_TIMER::m_lasttime
private

Definition at line 167 of file profile.h.

Referenced by SinceStart(), and Start().

◆ m_name

std::string PROF_TIMER::m_name
private

Definition at line 161 of file profile.h.

Referenced by Show(), and to_string().

◆ m_running

bool PROF_TIMER::m_running
private

Definition at line 162 of file profile.h.

Referenced by SinceStart(), Start(), and Stop().

◆ m_starttime

TIME_POINT PROF_TIMER::m_starttime
private

Definition at line 167 of file profile.h.

Referenced by SinceStart(), and Start().

◆ m_stoptime

TIME_POINT PROF_TIMER::m_stoptime
private

Definition at line 167 of file profile.h.

Referenced by SinceStart(), and Stop().


The documentation for this class was generated from the following file: