From 781c5f7e1d3b8e5aa059f3777c449d5227c58eed Mon Sep 17 00:00:00 2001 From: John Beard Date: Tue, 11 Jun 2019 22:16:54 +0100 Subject: [PATCH] Remove SCOPED_TIMER, use PROF_COUNTER SCOPED_TIMER is a QA-only simpler version of PROF_COUNTER. Extend PROF_COUNTER to allow access to the std::chrono::duration for more flexibility. Wrap PROF_COUNTER in SCOPED_PROF_COUNTER for RAII duration output (for the same effect as SCOPED_TIMER). --- include/profile.h | 98 ++++++++++++++----- .../tools/sexpr_parser/sexpr_parse.cpp | 2 +- qa/pcbnew_tools/tools/drc_tool/drc_tool.cpp | 4 +- .../tools/pcb_parser/pcb_parser_tool.cpp | 6 +- qa/qa_utils/include/qa_utils/scoped_timer.h | 59 ----------- 5 files changed, 78 insertions(+), 91 deletions(-) delete mode 100644 qa/qa_utils/include/qa_utils/scoped_timer.h diff --git a/include/profile.h b/include/profile.h index d7fcbe70b2..f54be78b3c 100644 --- a/include/profile.h +++ b/include/profile.h @@ -72,7 +72,7 @@ public: void Start() { m_running = true; - m_starttime = std::chrono::high_resolution_clock::now(); + m_starttime = CLOCK::now(); m_lasttime = m_starttime; } @@ -85,7 +85,8 @@ public: if( !m_running ) return; - m_stoptime = std::chrono::high_resolution_clock::now(); + m_stoptime = CLOCK::now(); + m_running = false; } /** @@ -93,13 +94,7 @@ public: */ void Show() { - TIME_POINT display_stoptime = m_running ? - std::chrono::high_resolution_clock::now() : - m_stoptime; - - std::chrono::duration elapsed = display_stoptime - m_starttime; - m_lasttime = display_stoptime; - std::cerr << m_name << " took " << elapsed.count() << " ms." << std::endl; + std::cerr << m_name << " took " << msecs() << "ms." << std::endl; } /** @@ -109,9 +104,7 @@ public: */ void Show( const std::string& aMessage ) { - TIME_POINT display_stoptime = m_running ? - std::chrono::high_resolution_clock::now() : - m_stoptime; + TIME_POINT display_stoptime = m_running ? CLOCK::now() : m_stoptime; std::chrono::duration elapsed = display_stoptime - m_starttime; std::chrono::duration delta_time = display_stoptime - m_lasttime; @@ -125,43 +118,94 @@ public: */ void ShowDlg() { - TIME_POINT display_stoptime = m_running ? - std::chrono::high_resolution_clock::now() : - m_stoptime; + const double ms = msecs(); - std::chrono::duration elapsed = display_stoptime - m_starttime; wxString msg; - if( elapsed.count() < 1000.0 ) - msg << m_name << " took " << elapsed.count() << " ms."; + if( ms < 1000.0 ) + msg << m_name << " took " << ms << " ms."; else - msg << m_name << " took " << elapsed.count()/1000.0 << " sec."; + msg << m_name << " took " << ms / 1000.0 << " sec."; wxLogMessage( msg ); } /** - * @return the elapsed time in ms + * @return 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. */ - double msecs() const + template + DURATION SinceStart( bool aSinceLast = false ) { - TIME_POINT stoptime = m_running ? - std::chrono::high_resolution_clock::now() : - m_stoptime; + const TIME_POINT stoptime = m_running ? CLOCK::now() : m_stoptime; + const TIME_POINT starttime = aSinceLast ? m_lasttime : m_starttime; - std::chrono::duration elapsed = stoptime - m_starttime; + m_lasttime = stoptime; - return elapsed.count(); + return std::chrono::duration_cast( stoptime - starttime ); + } + + /** + * @param aSinceLast: only get the time since the last time the time was read + * @return the elapsed time in ms since the timer was started. + */ + double msecs( bool aSinceLast = false ) + { + using DUR_MS = std::chrono::duration; + return SinceStart( aSinceLast ).count(); } private: std::string m_name; // a string printed in message bool m_running; - typedef std::chrono::time_point TIME_POINT; + using CLOCK = std::chrono::high_resolution_clock; + using TIME_POINT = std::chrono::time_point; TIME_POINT m_starttime, m_lasttime, m_stoptime; }; +/** + * 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_COUNTER 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 . + */ +template +class SCOPED_PROF_COUNTER +{ +public: + SCOPED_PROF_COUNTER( DURATION& aDuration ) : m_counter(), m_duration( aDuration ) + { + } + + ~SCOPED_PROF_COUNTER() + { + // update the output + m_duration = m_counter.SinceStart(); + } + +private: + ///< The counter to use to do the profiling + PROF_COUNTER m_counter; + + ///< The duration to update at the end of the scope + DURATION& m_duration; +}; + + /** * Function GetRunningMicroSecs * An alternate way to calculate an elapset time (in microsecondes) to class PROF_COUNTER diff --git a/qa/common_tools/tools/sexpr_parser/sexpr_parse.cpp b/qa/common_tools/tools/sexpr_parser/sexpr_parse.cpp index b5a23ec179..0084564a83 100644 --- a/qa/common_tools/tools/sexpr_parser/sexpr_parse.cpp +++ b/qa/common_tools/tools/sexpr_parser/sexpr_parse.cpp @@ -59,7 +59,7 @@ public: std::unique_ptr sexpr( m_parser.Parse( sexpr_str ) ); if( m_verbose ) - timer.Show( "S-Expression Parsing" ); + std::cout << "S-Expression Parsing took " << timer.msecs() << "ms" << std::endl; return sexpr != nullptr; } diff --git a/qa/pcbnew_tools/tools/drc_tool/drc_tool.cpp b/qa/pcbnew_tools/tools/drc_tool/drc_tool.cpp index 83ded9fa65..f8d6bca3b8 100644 --- a/qa/pcbnew_tools/tools/drc_tool/drc_tool.cpp +++ b/qa/pcbnew_tools/tools/drc_tool/drc_tool.cpp @@ -27,6 +27,7 @@ #include #include +#include #include @@ -36,7 +37,6 @@ #include #include -#include #include @@ -84,7 +84,7 @@ public: DRC_DURATION duration; { - SCOPED_TIMER timer( duration ); + SCOPED_PROF_COUNTER timer( duration ); drc_prov->RunDRC( aBoard ); } diff --git a/qa/pcbnew_tools/tools/pcb_parser/pcb_parser_tool.cpp b/qa/pcbnew_tools/tools/pcb_parser/pcb_parser_tool.cpp index 2bf77679ff..287d7bd76e 100644 --- a/qa/pcbnew_tools/tools/pcb_parser/pcb_parser_tool.cpp +++ b/qa/pcbnew_tools/tools/pcb_parser/pcb_parser_tool.cpp @@ -27,6 +27,7 @@ #include #include +#include #include @@ -37,7 +38,6 @@ #include -#include #include using PARSE_DURATION = std::chrono::microseconds; @@ -65,8 +65,10 @@ bool parse( std::istream& aStream, bool aVerbose ) try { - SCOPED_TIMER timer( duration ); + PROF_COUNTER timer; board = parser.Parse(); + + duration = timer.SinceStart(); } catch( const IO_ERROR& parse_error ) { diff --git a/qa/qa_utils/include/qa_utils/scoped_timer.h b/qa/qa_utils/include/qa_utils/scoped_timer.h deleted file mode 100644 index 78a855d5c6..0000000000 --- a/qa/qa_utils/include/qa_utils/scoped_timer.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * This program source code file is part of KiCad, a free EDA CAD application. - * - * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, you may find one here: - * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html - * or you may search the http://www.gnu.org website for the version 2 license, - * or you may write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifndef SCOPED_TIMER_H -#define SCOPED_TIMER_H - -#include - -/** - * 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 - */ -template class SCOPED_TIMER -{ - using CLOCK = std::chrono::steady_clock; - using TIME_PT = std::chrono::time_point; - -public: - SCOPED_TIMER( DURATION& aDuration ) : m_duration( aDuration ) - { - m_start = CLOCK::now(); - } - - ~SCOPED_TIMER() - { - const auto end = CLOCK::now(); - - // update the output - m_duration = std::chrono::duration_cast( end - m_start ); - } - -private: - DURATION& m_duration; - TIME_PT m_start; -}; - -#endif // SCOPED_TIMER_h \ No newline at end of file