KiCad PCB EDA Suite
SCOPED_PROF_TIMER< DURATION > Class Template Reference

A simple RAII class to measure the time of an operation. More...

#include <profile.h>

Inheritance diagram for SCOPED_PROF_TIMER< DURATION >:
PROF_TIMER

Public Member Functions

 SCOPED_PROF_TIMER (DURATION &aDuration)
 
 ~SCOPED_PROF_TIMER ()
 
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

PROF_TIMER m_counter
 < The counter to use to do the profiling More...
 
DURATION & m_duration
 
std::string m_name
 
bool m_running
 
TIME_POINT m_starttime
 
TIME_POINT m_lasttime
 
TIME_POINT m_stoptime
 

Detailed Description

template<typename DURATION>
class SCOPED_PROF_TIMER< DURATION >

A simple RAII class to measure the time of an operation.

On construction, a timer is started, and on destruction, the timer is ended, and the time difference is written into the given duration.

For example:

DURATION duration; // select a duration type as needed { SCOPED_PROF_TIMER<DURATION> timer( duration ); timed_activity(); } // duration is now the time timed activity took

From C++17, with class template argument deduction, you should be able to omit the <DURATION>.

Definition at line 190 of file profile.h.

Member Typedef Documentation

◆ CLOCK

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

Definition at line 164 of file profile.h.

◆ TIME_POINT

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

Definition at line 165 of file profile.h.

Constructor & Destructor Documentation

◆ SCOPED_PROF_TIMER()

template<typename DURATION >
SCOPED_PROF_TIMER< DURATION >::SCOPED_PROF_TIMER ( DURATION &  aDuration)
inline

Definition at line 193 of file profile.h.

193 : PROF_TIMER(), m_duration( aDuration )
194 {
195 }
PROF_TIMER()
Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition: profile.h:67
DURATION & m_duration
Definition: profile.h:208

◆ ~SCOPED_PROF_TIMER()

template<typename DURATION >
SCOPED_PROF_TIMER< DURATION >::~SCOPED_PROF_TIMER ( )
inline

Definition at line 197 of file profile.h.

198 {
199 // update the output
200 m_duration = m_counter.SinceStart<DURATION>();
201 }
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:133
PROF_TIMER m_counter
< The counter to use to do the profiling
Definition: profile.h:205

References SCOPED_PROF_TIMER< DURATION >::m_counter, SCOPED_PROF_TIMER< DURATION >::m_duration, and PROF_TIMER::SinceStart().

Member Function Documentation

◆ msecs()

double PROF_TIMER::msecs ( bool  aSinceLast = false)
inlineinherited
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 KIGFX::OPENGL_GAL::BeginDrawing(), E_SERIES::combine4(), KIGFX::CACHED_CONTAINER_GPU::defragmentResize(), KIGFX::CACHED_CONTAINER_RAM::defragmentResize(), KIGFX::CACHED_CONTAINER_GPU::defragmentResizeMemcpy(), KIGFX::GPU_NONCACHED_MANAGER::EndDrawing(), SCH_EDIT_FRAME::RecalculateConnections(), KIGFX::VIEW::Redraw(), PCB_SELECTION_TOOL::selectAllConnectedTracks(), and PROF_TIMER::to_string().

◆ Show()

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

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 PROF_TIMER::m_name.

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

◆ SinceStart()

template<typename DURATION >
DURATION PROF_TIMER::SinceStart ( bool  aSinceLast = false)
inlineinherited
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 }
TIME_POINT m_lasttime
Definition: profile.h:167
TIME_POINT m_stoptime
Definition: profile.h:167
bool m_running
Definition: profile.h:162
std::chrono::time_point< CLOCK > TIME_POINT
Definition: profile.h:165
TIME_POINT m_starttime
Definition: profile.h:167

References PROF_TIMER::m_lasttime, PROF_TIMER::m_running, PROF_TIMER::m_starttime, and PROF_TIMER::m_stoptime.

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

◆ Start()

void PROF_TIMER::Start ( )
inlineinherited

◆ Stop()

◆ to_string()

std::string PROF_TIMER::to_string ( )
inlineinherited

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

References PROF_TIMER::m_name, and PROF_TIMER::msecs().

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

Member Data Documentation

◆ m_counter

template<typename DURATION >
PROF_TIMER SCOPED_PROF_TIMER< DURATION >::m_counter
private

< The counter to use to do the profiling

The duration to update at the end of the scope

Definition at line 205 of file profile.h.

Referenced by SCOPED_PROF_TIMER< DURATION >::~SCOPED_PROF_TIMER().

◆ m_duration

template<typename DURATION >
DURATION& SCOPED_PROF_TIMER< DURATION >::m_duration
private

Definition at line 208 of file profile.h.

Referenced by SCOPED_PROF_TIMER< DURATION >::~SCOPED_PROF_TIMER().

◆ m_lasttime

TIME_POINT PROF_TIMER::m_lasttime
privateinherited

Definition at line 167 of file profile.h.

Referenced by PROF_TIMER::SinceStart(), and PROF_TIMER::Start().

◆ m_name

std::string PROF_TIMER::m_name
privateinherited

Definition at line 161 of file profile.h.

Referenced by PROF_TIMER::Show(), and PROF_TIMER::to_string().

◆ m_running

bool PROF_TIMER::m_running
privateinherited

Definition at line 162 of file profile.h.

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

◆ m_starttime

TIME_POINT PROF_TIMER::m_starttime
privateinherited

Definition at line 167 of file profile.h.

Referenced by PROF_TIMER::SinceStart(), and PROF_TIMER::Start().

◆ m_stoptime

TIME_POINT PROF_TIMER::m_stoptime
privateinherited

Definition at line 167 of file profile.h.

Referenced by PROF_TIMER::SinceStart(), and PROF_TIMER::Stop().


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