KiCad PCB EDA Suite
profile.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * 2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #ifndef TPROFILE_H
32 #define TPROFILE_H
33 
34 #include <chrono>
35 #include <string>
36 #include <iostream>
37 #include <iomanip>
38 
45 {
46 public:
52  PROF_COUNTER( const std::string& aName, bool aAutostart = true ) :
53  m_name( aName ), m_running( false )
54  {
55  if( aAutostart )
56  Start();
57  }
58 
64  {
65  Start();
66  }
67 
71  void Start()
72  {
73  m_running = true;
74  m_starttime = CLOCK::now();
76  }
77 
78 
82  void Stop()
83  {
84  if( !m_running )
85  return;
86 
87  m_stoptime = CLOCK::now();
88  m_running = false;
89  }
90 
99  void Show( std::ostream& aStream = std::cerr )
100  {
101  using DURATION = std::chrono::duration<double, std::nano>;
102 
103  const auto duration = SinceStart<DURATION>();
104  const double cnt = duration.count();
105 
106  if( m_name.size() )
107  {
108  aStream << m_name << " took ";
109  }
110 
111  if( cnt < 1e3 )
112  aStream << cnt << "ns";
113  else if( cnt < 1e6 )
114  aStream << cnt / 1e3 << "┬Ás";
115  else if( cnt < 1e9 )
116  aStream << cnt / 1e6 << "ms";
117  else
118  aStream << cnt / 1e9 << "s";
119 
120  aStream << std::endl;
121  }
122 
128  template <typename DURATION>
129  DURATION SinceStart( bool aSinceLast = false )
130  {
131  const TIME_POINT stoptime = m_running ? CLOCK::now() : m_stoptime;
132  const TIME_POINT starttime = aSinceLast ? m_lasttime : m_starttime;
133 
134  m_lasttime = stoptime;
135 
136  return std::chrono::duration_cast<DURATION>( stoptime - starttime );
137  }
138 
143  double msecs( bool aSinceLast = false )
144  {
145  using DUR_MS = std::chrono::duration<double, std::milli>;
146  return SinceStart<DUR_MS>( aSinceLast ).count();
147  }
148 
149 private:
150  std::string m_name; // a string printed in message
151  bool m_running;
152 
153  using CLOCK = std::chrono::high_resolution_clock;
154  using TIME_POINT = std::chrono::time_point<CLOCK>;
155 
157 };
158 
159 
178 template <typename DURATION>
180 {
181 public:
182  SCOPED_PROF_COUNTER( DURATION& aDuration ) : m_counter(), m_duration( aDuration )
183  {
184  }
185 
187  {
188  // update the output
189  m_duration = m_counter.SinceStart<DURATION>();
190  }
191 
192 private:
195 
197  DURATION& m_duration;
198 };
199 
200 
208 unsigned GetRunningMicroSecs();
209 
210 #endif // TPROFILE_H
void Stop()
save the time when this function was called, and set the counter stane to stop
Definition: profile.h:82
std::chrono::time_point< CLOCK > TIME_POINT
Definition: profile.h:154
double msecs(bool aSinceLast=false)
Definition: profile.h:143
TIME_POINT m_lasttime
Definition: profile.h:156
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:44
std::string m_name
Definition: profile.h:150
TIME_POINT m_stoptime
Definition: profile.h:156
std::chrono::high_resolution_clock CLOCK
Definition: profile.h:153
PROF_COUNTER()
Creates a PROF_COUNTER for measuring an elapsed time in milliseconds The counter is started and the s...
Definition: profile.h:63
SCOPED_PROF_COUNTER(DURATION &aDuration)
Definition: profile.h:182
PROF_COUNTER m_counter
< The counter to use to do the profiling
Definition: profile.h:194
bool m_running
Definition: profile.h:151
A simple RAII class to measure the time of an operation.
Definition: profile.h:179
TIME_POINT m_starttime
Definition: profile.h:156
unsigned GetRunningMicroSecs()
Function GetRunningMicroSecs An alternate way to calculate an elapset time (in microsecondes) to clas...
PROF_COUNTER(const std::string &aName, bool aAutostart=true)
Creates a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition: profile.h:52
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:129
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
Definition: profile.h:99
void Start()
Starts or restarts the counter.
Definition: profile.h:71
DURATION & m_duration
Definition: profile.h:197