kicad/include/profile.h

94 lines
2.5 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>
2016-12-31 07:15:20 +00:00
* 2016 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
2016-11-04 21:29:42 +00:00
class PROF_COUNTER
{
public:
2016-12-31 07:15:20 +00:00
PROF_COUNTER( const std::string& name, bool autostart = true ) :
m_name( name ),
m_running( false )
2016-12-09 11:04:32 +00:00
{
if( autostart )
start();
}
void start()
{
m_running = true;
2016-12-31 07:15:20 +00:00
starttime = std::chrono::system_clock::now();
2016-12-09 11:04:32 +00:00
}
void stop()
{
if( !m_running )
return;
2016-12-31 07:15:20 +00:00
stoptime = std::chrono::system_clock::now();
2016-12-09 11:04:32 +00:00
}
void show()
{
2016-12-31 07:15:20 +00:00
time_point display_stoptime;
if( m_running )
display_stoptime = std::chrono::system_clock::now();
else
display_stoptime = stoptime;
std::chrono::duration<double, std::milli> d = display_stoptime - starttime;
std::cerr << m_name << " took " << std::setprecision(1) << d.count() << "milliseconds." << std::endl;
2016-12-09 11:04:32 +00:00
}
double msecs() const
{
2016-12-31 07:15:20 +00:00
std::chrono::duration<double, std::milli> d = stoptime - starttime;
return d.count();
2016-12-09 11:04:32 +00:00
}
2016-11-04 21:29:42 +00:00
private:
2016-12-09 11:04:32 +00:00
std::string m_name;
bool m_running;
2016-12-31 07:15:20 +00:00
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
time_point starttime, stoptime;
2016-11-04 21:29:42 +00:00
};
2013-09-18 17:56:37 +00:00
#endif