Move timer code to own cpp/h files.

This commit is contained in:
Kimmo Varis 2010-08-31 23:18:07 +03:00
parent 9a52b35144
commit d417256c98
12 changed files with 233 additions and 132 deletions

View File

@ -272,6 +272,10 @@
RelativePath="threadexecutor.cpp"
>
</File>
<File
RelativePath="..\lib\timer.cpp"
>
</File>
<File
RelativePath="..\lib\token.cpp"
>
@ -370,6 +374,10 @@
RelativePath="..\lib\settings.h"
>
</File>
<File
RelativePath="..\lib\timer.h"
>
</File>
<File
RelativePath="..\lib\token.h"
>

View File

@ -140,6 +140,7 @@
<ClCompile Include="..\lib\path.cpp" />
<ClCompile Include="..\lib\preprocessor.cpp" />
<ClCompile Include="..\lib\settings.cpp" />
<ClCompile Include="..\lib\timer.cpp" />
<ClCompile Include="..\lib\token.cpp" />
<ClCompile Include="..\lib\tokenize.cpp" />
<ClCompile Include="cppcheckexecutor.cpp" />
@ -166,6 +167,7 @@
<ClInclude Include="..\lib\path.h" />
<ClInclude Include="..\lib\preprocessor.h" />
<ClInclude Include="..\lib\settings.h" />
<ClInclude Include="..\lib\timer.h" />
<ClInclude Include="..\lib\token.h" />
<ClInclude Include="..\lib\tokenize.h" />
<ClInclude Include="resource.h" />

View File

@ -77,6 +77,9 @@
<ClCompile Include="..\lib\path.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h">
@ -145,6 +148,9 @@
<ClInclude Include="..\lib\path.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\lib\timer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="cppcheck.rc" />

View File

@ -32,141 +32,10 @@
#include <fstream>
#include <stdexcept>
#include <ctime>
/*
TODO:
- handle SHOWTIME_TOP5 in TimerResults
- sort list by time
- do not sort the results alphabetically
- rename "file" to "single"
- synchronise map access in multithreaded mode or disable timing
- add unit tests
- for --showtime (needs input file)
- for Timer* classes
- move timer stuff to seperate source/header
*/
enum
{
SHOWTIME_NONE = 0,
SHOWTIME_FILE,
SHOWTIME_SUMMARY,
SHOWTIME_TOP5
};
class TimerResultsIntf
{
public:
virtual ~TimerResultsIntf() { }
virtual void AddResults(const std::string& str, std::clock_t clocks) = 0;
};
struct TimerResultsData
{
std::clock_t _clocks;
long _numberOfResults;
TimerResultsData()
: _clocks(0)
, _numberOfResults(0)
{
}
};
class TimerResults : public TimerResultsIntf
{
public:
TimerResults()
{
}
void ShowResults()
{
std::clock_t overallClocks = 0;
std::map<std::string, struct TimerResultsData>::const_iterator I = _results.begin();
const std::map<std::string, struct TimerResultsData>::const_iterator E = _results.end();
while (I != E)
{
const double sec = (double)I->second._clocks / CLOCKS_PER_SEC;
const double secAverage = (double)(I->second._clocks / I->second._numberOfResults) / CLOCKS_PER_SEC;
std::cout << I->first << ": " << sec << "s (avg. " << secAverage << "s - " << I->second._numberOfResults << " result(s))" << std::endl;
overallClocks += I->second._clocks;
++I;
}
const double secOverall = (double)overallClocks / CLOCKS_PER_SEC;
std::cout << "Overall time: " << secOverall << "s" << std::endl;
}
virtual void AddResults(const std::string& str, std::clock_t clocks)
{
_results[str]._clocks += clocks;
_results[str]._numberOfResults++;
}
private:
std::map<std::string, struct TimerResultsData> _results;
};
#include "timer.h"
static TimerResults S_timerResults;
class Timer
{
public:
Timer(const std::string& str, unsigned int showtimeMode, TimerResultsIntf* timerResults = NULL)
: _str(str)
, _showtimeMode(showtimeMode)
, _start(0)
, _stopped(false)
, _timerResults(timerResults)
{
if (showtimeMode != SHOWTIME_NONE)
_start = std::clock();
}
~Timer()
{
Stop();
}
void Stop()
{
if ((_showtimeMode != SHOWTIME_NONE) && !_stopped)
{
const std::clock_t end = std::clock();
const std::clock_t diff = end - _start;
if (_showtimeMode == SHOWTIME_FILE)
{
double sec = (double)diff / CLOCKS_PER_SEC;
std::cout << _str << ": " << sec << "s" << std::endl;
}
else
{
if (_timerResults)
_timerResults->AddResults(_str, diff);
}
}
_stopped = true;
}
private:
Timer& operator=(const Timer&); // disallow assignments
const std::string _str;
const unsigned int _showtimeMode;
std::clock_t _start;
bool _stopped;
TimerResultsIntf* _timerResults;
};
//---------------------------------------------------------------------------
CppCheck::CppCheck(ErrorLogger &errorLogger)
: _errorLogger(errorLogger)
{

View File

@ -222,6 +222,10 @@
RelativePath=".\settings.cpp"
>
</File>
<File
RelativePath=".\timer.cpp"
>
</File>
<File
RelativePath=".\token.cpp"
>
@ -316,6 +320,10 @@
RelativePath=".\settings.h"
>
</File>
<File
RelativePath=".\timer.h"
>
</File>
<File
RelativePath=".\token.h"
>

View File

@ -102,6 +102,7 @@
<ClCompile Include="path.cpp" />
<ClCompile Include="preprocessor.cpp" />
<ClCompile Include="settings.cpp" />
<ClCompile Include="timer.cpp" />
<ClCompile Include="token.cpp" />
<ClCompile Include="tokenize.cpp" />
</ItemGroup>
@ -125,6 +126,7 @@
<ClInclude Include="path.h" />
<ClInclude Include="preprocessor.h" />
<ClInclude Include="settings.h" />
<ClInclude Include="timer.h" />
<ClInclude Include="token.h" />
<ClInclude Include="tokenize.h" />
</ItemGroup>

View File

@ -68,6 +68,9 @@
<ClCompile Include="path.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="check.h">
@ -133,5 +136,8 @@
<ClInclude Include="path.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="timer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

98
lib/timer.cpp Normal file
View File

@ -0,0 +1,98 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "timer.h"
/*
TODO:
- handle SHOWTIME_TOP5 in TimerResults
- sort list by time
- do not sort the results alphabetically
- rename "file" to "single"
- synchronise map access in multithreaded mode or disable timing
- add unit tests
- for --showtime (needs input file)
- for Timer* classes
*/
void TimerResults::ShowResults()
{
std::clock_t overallClocks = 0;
std::map<std::string, struct TimerResultsData>::const_iterator I = _results.begin();
const std::map<std::string, struct TimerResultsData>::const_iterator E = _results.end();
while (I != E)
{
const double sec = (double)I->second._clocks / CLOCKS_PER_SEC;
const double secAverage = (double)(I->second._clocks / I->second._numberOfResults) / CLOCKS_PER_SEC;
std::cout << I->first << ": " << sec << "s (avg. " << secAverage << "s - " << I->second._numberOfResults << " result(s))" << std::endl;
overallClocks += I->second._clocks;
++I;
}
const double secOverall = (double)overallClocks / CLOCKS_PER_SEC;
std::cout << "Overall time: " << secOverall << "s" << std::endl;
}
void TimerResults::AddResults(const std::string& str, std::clock_t clocks)
{
_results[str]._clocks += clocks;
_results[str]._numberOfResults++;
}
Timer::Timer(const std::string& str, unsigned int showtimeMode, TimerResultsIntf* timerResults)
: _str(str)
, _showtimeMode(showtimeMode)
, _start(0)
, _stopped(false)
, _timerResults(timerResults)
{
if (showtimeMode != SHOWTIME_NONE)
_start = std::clock();
}
Timer::~Timer()
{
Stop();
}
void Timer::Stop()
{
if ((_showtimeMode != SHOWTIME_NONE) && !_stopped)
{
const std::clock_t end = std::clock();
const std::clock_t diff = end - _start;
if (_showtimeMode == SHOWTIME_FILE)
{
double sec = (double)diff / CLOCKS_PER_SEC;
std::cout << _str << ": " << sec << "s" << std::endl;
}
else
{
if (_timerResults)
_timerResults->AddResults(_str, diff);
}
}
_stopped = true;
}

86
lib/timer.h Normal file
View File

@ -0,0 +1,86 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef TIMER_H
#define TIMER_H
#include <string>
#include <map>
#include <ctime>
enum
{
SHOWTIME_NONE = 0,
SHOWTIME_FILE,
SHOWTIME_SUMMARY,
SHOWTIME_TOP5
};
class TimerResultsIntf
{
public:
virtual ~TimerResultsIntf() { }
virtual void AddResults(const std::string& str, std::clock_t clocks) = 0;
};
struct TimerResultsData
{
std::clock_t _clocks;
long _numberOfResults;
TimerResultsData()
: _clocks(0)
, _numberOfResults(0)
{
}
};
class TimerResults : public TimerResultsIntf
{
public:
TimerResults()
{
}
void ShowResults();
virtual void AddResults(const std::string& str, std::clock_t clocks);
private:
std::map<std::string, struct TimerResultsData> _results;
};
class Timer
{
public:
Timer(const std::string& str, unsigned int showtimeMode, TimerResultsIntf* timerResults = NULL);
~Timer();
void Stop();
private:
Timer& operator=(const Timer&); // disallow assignments
const std::string _str;
const unsigned int _showtimeMode;
std::clock_t _start;
bool _stopped;
TimerResultsIntf* _timerResults;
};
#endif // TIMER_H

View File

@ -354,6 +354,10 @@
RelativePath="testunusedvar.cpp"
>
</File>
<File
RelativePath="..\lib\timer.cpp"
>
</File>
<File
RelativePath=".\tinyxml\tinystr.cpp"
>
@ -464,6 +468,10 @@
RelativePath="testsuite.h"
>
</File>
<File
RelativePath="..\lib\timer.h"
>
</File>
<File
RelativePath=".\tinyxml\tinystr.h"
>

View File

@ -137,6 +137,7 @@
<ClCompile Include="..\lib\path.cpp" />
<ClCompile Include="..\lib\preprocessor.cpp" />
<ClCompile Include="..\lib\settings.cpp" />
<ClCompile Include="..\lib\timer.cpp" />
<ClCompile Include="..\lib\token.cpp" />
<ClCompile Include="..\lib\tokenize.cpp" />
<ClCompile Include="testautovariables.cpp" />
@ -187,6 +188,7 @@
<ClInclude Include="..\lib\path.h" />
<ClInclude Include="..\lib\preprocessor.h" />
<ClInclude Include="..\lib\settings.h" />
<ClInclude Include="..\lib\timer.h" />
<ClInclude Include="..\lib\token.h" />
<ClInclude Include="..\lib\tokenize.h" />
<ClInclude Include="testsuite.h" />

View File

@ -149,6 +149,9 @@
<ClCompile Include="tinyxml\tinyxmlparser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="testsuite.h">
@ -220,5 +223,8 @@
<ClInclude Include="tinyxml\tinyxml.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\lib\timer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>