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  * 2017 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 #include <wx/log.h>
39 
46 {
47 public:
53  PROF_COUNTER( const std::string& aName, bool aAutostart = true ) :
54  m_name( aName ), m_running( false )
55  {
56  if( aAutostart )
57  Start();
58  }
59 
65  {
66  Start();
67  }
68 
72  void Start()
73  {
74  m_running = true;
75  m_starttime = std::chrono::high_resolution_clock::now();
76  }
77 
78 
82  void Stop()
83  {
84  if( !m_running )
85  return;
86 
87  m_stoptime = std::chrono::high_resolution_clock::now();
88  }
89 
93  void Show()
94  {
95  TIME_POINT display_stoptime = m_running ?
96  std::chrono::high_resolution_clock::now() :
97  m_stoptime;
98 
99  std::chrono::duration<double, std::milli> elapsed = display_stoptime - m_starttime;
100  std::cerr << m_name << " took " << elapsed.count() << " milliseconds." << std::endl;
101  }
102 
106  void ShowDlg()
107  {
108  TIME_POINT display_stoptime = m_running ?
109  std::chrono::high_resolution_clock::now() :
110  m_stoptime;
111 
112  std::chrono::duration<double, std::milli> elapsed = display_stoptime - m_starttime;
113  wxString msg;
114  msg << m_name << " took " << elapsed.count() << " ms.";
115  wxLogMessage( msg );
116  }
117 
121  double msecs() const
122  {
123  TIME_POINT stoptime = m_running ?
124  std::chrono::high_resolution_clock::now() :
125  m_stoptime;
126 
127  std::chrono::duration<double, std::milli> elapsed = stoptime - m_starttime;
128 
129  return elapsed.count();
130  }
131 
132 private:
133  std::string m_name; // a string printed in message
134  bool m_running;
135 
136  typedef std::chrono::time_point<std::chrono::high_resolution_clock> TIME_POINT;
137 
138  TIME_POINT m_starttime, m_stoptime;
139 };
140 
141 
149 unsigned GetRunningMicroSecs();
150 
151 #endif
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< std::chrono::high_resolution_clock > TIME_POINT
Definition: profile.h:136
void Show()
Print the elapsed time (in ms) to STDERR.
Definition: profile.h:93
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:45
std::string m_name
Definition: profile.h:133
TIME_POINT m_stoptime
Definition: profile.h:138
void ShowDlg()
Show the elapsed time (in ms) in a wxLogMessage window.
Definition: profile.h:106
PROF_COUNTER()
Creates a PROF_COUNTER for measuring an elapsed time in milliseconds The counter is started and the s...
Definition: profile.h:64
bool m_running
Definition: profile.h:134
TIME_POINT m_starttime
Definition: profile.h:138
double msecs() const
Definition: profile.h:121
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:53
void Start()
Starts or restarts the counter.
Definition: profile.h:72