kicad/include/profile.h

211 lines
5.8 KiB
C
Raw Normal View History

2013-09-18 17:56:37 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
2019-03-14 08:11:34 +00:00
* 2019 KiCad Developers, see AUTHORS.txt for contributors.
2013-09-18 17:56:37 +00:00
*
* 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
*/
/**
* @file profile.h:
* @brief Simple profiling functions for measuring code execution time.
*/
#ifndef __TPROFILE_H
#define __TPROFILE_H
2016-12-31 07:15:20 +00:00
#include <chrono>
2016-11-04 21:29:42 +00:00
#include <string>
2016-12-31 07:15:20 +00:00
#include <iostream>
#include <iomanip>
2013-09-18 17:56:37 +00:00
/**
* The class PROF_COUNTER is a small class to help profiling.
* It allows the calculation of the elapsed time (in milliseconds) between
* its creation (or the last call to Start() ) and the last call to Stop()
*/
2016-11-04 21:29:42 +00:00
class PROF_COUNTER
{
public:
/**
* Creates a PROF_COUNTER for measuring an elapsed time in milliseconds
* @param aName = a string that will be printed in message.
* @param aAutostart = true (default) to immediately start the timer
*/
PROF_COUNTER( const std::string& aName, bool aAutostart = true ) :
m_name( aName ), m_running( false )
{
if( aAutostart )
Start();
}
/**
* Creates a PROF_COUNTER for measuring an elapsed time in milliseconds
* The counter is started and the string to print in message is left empty.
*/
PROF_COUNTER()
2016-12-09 11:04:32 +00:00
{
Start();
2016-12-09 11:04:32 +00:00
}
/**
* Starts or restarts the counter
*/
void Start()
2016-12-09 11:04:32 +00:00
{
m_running = true;
m_starttime = CLOCK::now();
2019-03-14 08:11:34 +00:00
m_lasttime = m_starttime;
2016-12-09 11:04:32 +00:00
}
/**
* save the time when this function was called, and set the counter stane to stop
*/
void Stop()
2016-12-09 11:04:32 +00:00
{
if( !m_running )
return;
m_stoptime = CLOCK::now();
m_running = false;
2016-12-09 11:04:32 +00:00
}
/**
* 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.
*
* @param the stream to print to.
*/
void Show( std::ostream& aStream = std::cerr )
2016-12-09 11:04:32 +00:00
{
using DURATION = std::chrono::duration<double, std::nano>;
const auto duration = SinceStart<DURATION>();
const double cnt = duration.count();
if( m_name.size() )
{
aStream << m_name << " took ";
}
if( cnt < 1e3 )
aStream << cnt << "ns";
else if( cnt < 1e6 )
aStream << cnt / 1e3 << "µs";
else if( cnt < 1e9 )
aStream << cnt / 1e6 << "ms";
else
aStream << cnt / 1e9 << "s";
aStream << std::endl;
2019-03-14 08:11:34 +00:00
}
/**
* @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.
*/
template <typename DURATION>
DURATION SinceStart( bool aSinceLast = false )
2016-12-09 11:04:32 +00:00
{
const TIME_POINT stoptime = m_running ? CLOCK::now() : m_stoptime;
const TIME_POINT starttime = aSinceLast ? m_lasttime : m_starttime;
m_lasttime = stoptime;
return std::chrono::duration_cast<DURATION>( 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<double, std::milli>;
return SinceStart<DUR_MS>( aSinceLast ).count();
2016-12-09 11:04:32 +00:00
}
2016-11-04 21:29:42 +00:00
private:
std::string m_name; // a string printed in message
2016-12-09 11:04:32 +00:00
bool m_running;
2016-12-31 07:15:20 +00:00
using CLOCK = std::chrono::high_resolution_clock;
using TIME_POINT = std::chrono::time_point<CLOCK>;
2016-12-31 07:15:20 +00:00
2019-03-14 08:11:34 +00:00
TIME_POINT m_starttime, m_lasttime, m_stoptime;
2016-11-04 21:29:42 +00:00
};
/**
* 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<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>.
*/
template <typename DURATION>
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<DURATION>();
}
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
* @return an ever increasing indication of elapsed microseconds.
* Use this by computing differences between two calls.
* @author Dick Hollenbeck
*/
unsigned GetRunningMicroSecs();
2013-09-18 17:56:37 +00:00
#endif