KiCad PCB EDA Suite
PROF_COUNTER Class Reference

A small class to help profiling. More...

#include <profile.h>

Public Member Functions

 PROF_COUNTER (const std::string &aName, bool aAutostart=true)
 Create a PROF_COUNTER for measuring an elapsed time in milliseconds. More...
 
 PROF_COUNTER ()
 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)
 

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 45 of file profile.h.

Member Typedef Documentation

◆ CLOCK

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

Definition at line 156 of file profile.h.

◆ TIME_POINT

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

Definition at line 157 of file profile.h.

Constructor & Destructor Documentation

◆ PROF_COUNTER() [1/2]

PROF_COUNTER::PROF_COUNTER ( 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 54 of file profile.h.

54  :
55  m_name( aName ), m_running( false )
56  {
57  if( aAutostart )
58  Start();
59  }
std::string m_name
Definition: profile.h:153
bool m_running
Definition: profile.h:154
void Start()
Start or restart the counter.
Definition: profile.h:74

References Start().

◆ PROF_COUNTER() [2/2]

PROF_COUNTER::PROF_COUNTER ( )
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 66 of file profile.h.

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

References Start().

Member Function Documentation

◆ msecs()

double PROF_COUNTER::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 146 of file profile.h.

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

Referenced by KIGFX::OPENGL_GAL::beginDrawing(), eserie::combine4(), KIGFX::CACHED_CONTAINER_GPU::defragmentResize(), KIGFX::CACHED_CONTAINER_RAM::defragmentResize(), KIGFX::CACHED_CONTAINER_GPU::defragmentResizeMemcpy(), EDA_DRAW_PANEL_GAL::DoRePaint(), KIGFX::GPU_CACHED_MANAGER::EndDrawing(), KIGFX::GPU_NONCACHED_MANAGER::EndDrawing(), KIGFX::OPENGL_GAL::endDrawing(), QA_SEXPR_PARSER::Parse(), CONNECTION_GRAPH::Recalculate(), SCH_EDIT_FRAME::RecalculateConnections(), and KIGFX::VIEW::Redraw().

◆ Show()

void PROF_COUNTER::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 102 of file profile.h.

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

References m_name.

Referenced by RN_NET::compute(), SCH_EDIT_FRAME::OpenProjectFiles(), polygon_triangulation_main(), CONNECTION_GRAPH::Recalculate(), CN_CONNECTIVITY_ALGO::searchConnections(), and CONNECTIVITY_DATA::updateRatsnest().

◆ SinceStart()

template<typename DURATION >
DURATION PROF_COUNTER::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 132 of file profile.h.

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

References m_lasttime, m_running, m_starttime, and m_stoptime.

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

◆ Start()

void PROF_COUNTER::Start ( )
inline

Start or restart the counter.

Definition at line 74 of file profile.h.

75  {
76  m_running = true;
77  m_starttime = CLOCK::now();
79  }
TIME_POINT m_lasttime
Definition: profile.h:159
bool m_running
Definition: profile.h:154
TIME_POINT m_starttime
Definition: profile.h:159

References m_lasttime, m_running, and m_starttime.

Referenced by PROF_COUNTER().

◆ Stop()

Member Data Documentation

◆ m_lasttime

TIME_POINT PROF_COUNTER::m_lasttime
private

Definition at line 159 of file profile.h.

Referenced by SinceStart(), and Start().

◆ m_name

std::string PROF_COUNTER::m_name
private

Definition at line 153 of file profile.h.

Referenced by Show().

◆ m_running

bool PROF_COUNTER::m_running
private

Definition at line 154 of file profile.h.

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

◆ m_starttime

TIME_POINT PROF_COUNTER::m_starttime
private

Definition at line 159 of file profile.h.

Referenced by SinceStart(), and Start().

◆ m_stoptime

TIME_POINT PROF_COUNTER::m_stoptime
private

Definition at line 159 of file profile.h.

Referenced by SinceStart(), and Stop().


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