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  * Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <[email protected]>
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 
46 {
47 public:
54  PROF_COUNTER( const std::string& aName, bool aAutostart = true ) :
55  m_name( aName ), m_running( false )
56  {
57  if( aAutostart )
58  Start();
59  }
60 
67  {
68  Start();
69  }
70 
74  void Start()
75  {
76  m_running = true;
77  m_starttime = CLOCK::now();
79  }
80 
81 
85  void Stop()
86  {
87  if( !m_running )
88  return;
89 
90  m_stoptime = CLOCK::now();
91  m_running = false;
92  }
93 
102  void Show( std::ostream& aStream = std::cerr )
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  }
125 
131  template <typename DURATION>
132  DURATION SinceStart( bool aSinceLast = false )
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  }
141 
146  double msecs( bool aSinceLast = false )
147  {
148  using DUR_MS = std::chrono::duration<double, std::milli>;
149  return SinceStart<DUR_MS>( aSinceLast ).count();
150  }
151 
152 private:
153  std::string m_name; // a string printed in message
154  bool m_running;
155 
156  using CLOCK = std::chrono::high_resolution_clock;
157  using TIME_POINT = std::chrono::time_point<CLOCK>;
158 
160 };
161 
162 
181 template <typename DURATION>
183 {
184 public:
185  SCOPED_PROF_COUNTER( DURATION& aDuration ) : m_counter(), m_duration( aDuration )
186  {
187  }
188 
190  {
191  // update the output
192  m_duration = m_counter.SinceStart<DURATION>();
193  }
194 
195 private:
198 
200  DURATION& m_duration;
201 };
202 
203 
211 unsigned GetRunningMicroSecs();
212 
213 #endif // TPROFILE_H
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition: profile.h:85
std::chrono::time_point< CLOCK > TIME_POINT
Definition: profile.h:157
double msecs(bool aSinceLast=false)
Definition: profile.h:146
TIME_POINT m_lasttime
Definition: profile.h:159
A small class to help profiling.
Definition: profile.h:45
std::string m_name
Definition: profile.h:153
TIME_POINT m_stoptime
Definition: profile.h:159
std::chrono::high_resolution_clock CLOCK
Definition: profile.h:156
PROF_COUNTER()
Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition: profile.h:66
SCOPED_PROF_COUNTER(DURATION &aDuration)
Definition: profile.h:185
PROF_COUNTER m_counter
< The counter to use to do the profiling
Definition: profile.h:197
bool m_running
Definition: profile.h:154
A simple RAII class to measure the time of an operation.
Definition: profile.h:182
TIME_POINT m_starttime
Definition: profile.h:159
unsigned GetRunningMicroSecs()
An alternate way to calculate an elapset time (in microsecondes) to class PROF_COUNTER.
PROF_COUNTER(const std::string &aName, bool aAutostart=true)
Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition: profile.h:54
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:132
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
Definition: profile.h:102
void Start()
Start or restart the counter.
Definition: profile.h:74
DURATION & m_duration
Definition: profile.h:200