From d417256c9810ae78ee36b3f4f652cf926caba224 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Tue, 31 Aug 2010 23:18:07 +0300 Subject: [PATCH] Move timer code to own cpp/h files. --- cli/cppcheck.vcproj | 8 +++ cli/cppcheck.vcxproj | 2 + cli/cppcheck.vcxproj.filters | 6 ++ lib/cppcheck.cpp | 133 +---------------------------------- lib/lib.vcproj | 8 +++ lib/lib.vcxproj | 2 + lib/lib.vcxproj.filters | 6 ++ lib/timer.cpp | 98 ++++++++++++++++++++++++++ lib/timer.h | 86 ++++++++++++++++++++++ test/test.vcproj | 8 +++ test/test.vcxproj | 2 + test/test.vcxproj.filters | 6 ++ 12 files changed, 233 insertions(+), 132 deletions(-) create mode 100644 lib/timer.cpp create mode 100644 lib/timer.h diff --git a/cli/cppcheck.vcproj b/cli/cppcheck.vcproj index 671b58203..48a52d76c 100755 --- a/cli/cppcheck.vcproj +++ b/cli/cppcheck.vcproj @@ -272,6 +272,10 @@ RelativePath="threadexecutor.cpp" > + + @@ -370,6 +374,10 @@ RelativePath="..\lib\settings.h" > + + diff --git a/cli/cppcheck.vcxproj b/cli/cppcheck.vcxproj index e38123848..b68daecd9 100644 --- a/cli/cppcheck.vcxproj +++ b/cli/cppcheck.vcxproj @@ -140,6 +140,7 @@ + @@ -166,6 +167,7 @@ + diff --git a/cli/cppcheck.vcxproj.filters b/cli/cppcheck.vcxproj.filters index ca3ed20c6..466d3457d 100644 --- a/cli/cppcheck.vcxproj.filters +++ b/cli/cppcheck.vcxproj.filters @@ -77,6 +77,9 @@ Source Files + + Source Files + @@ -145,6 +148,9 @@ Header Files + + Header Files + diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 052bc456b..db0dda322 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -32,141 +32,10 @@ #include #include #include - -/* - 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::const_iterator I = _results.begin(); - const std::map::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 _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) { diff --git a/lib/lib.vcproj b/lib/lib.vcproj index 839a46a74..8147d5efa 100644 --- a/lib/lib.vcproj +++ b/lib/lib.vcproj @@ -222,6 +222,10 @@ RelativePath=".\settings.cpp" > + + @@ -316,6 +320,10 @@ RelativePath=".\settings.h" > + + diff --git a/lib/lib.vcxproj b/lib/lib.vcxproj index ae3d4301c..79a18c588 100644 --- a/lib/lib.vcxproj +++ b/lib/lib.vcxproj @@ -102,6 +102,7 @@ + @@ -125,6 +126,7 @@ + diff --git a/lib/lib.vcxproj.filters b/lib/lib.vcxproj.filters index ca84610fd..a79930aeb 100644 --- a/lib/lib.vcxproj.filters +++ b/lib/lib.vcxproj.filters @@ -68,6 +68,9 @@ Source Files + + Source Files + @@ -133,5 +136,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/lib/timer.cpp b/lib/timer.cpp new file mode 100644 index 000000000..626fdcb7b --- /dev/null +++ b/lib/timer.cpp @@ -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 . + */ +#include +#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::const_iterator I = _results.begin(); + const std::map::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; +} diff --git a/lib/timer.h b/lib/timer.h new file mode 100644 index 000000000..a88a334d7 --- /dev/null +++ b/lib/timer.h @@ -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 . + */ + +#ifndef TIMER_H +#define TIMER_H + +#include +#include +#include + +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 _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 diff --git a/test/test.vcproj b/test/test.vcproj index 23681c010..c7c61cc27 100755 --- a/test/test.vcproj +++ b/test/test.vcproj @@ -354,6 +354,10 @@ RelativePath="testunusedvar.cpp" > + + @@ -464,6 +468,10 @@ RelativePath="testsuite.h" > + + diff --git a/test/test.vcxproj b/test/test.vcxproj index b824f9083..87f5a0172 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -137,6 +137,7 @@ + @@ -187,6 +188,7 @@ + diff --git a/test/test.vcxproj.filters b/test/test.vcxproj.filters index e8d593e2e..ff7a82552 100644 --- a/test/test.vcxproj.filters +++ b/test/test.vcxproj.filters @@ -149,6 +149,9 @@ Source Files + + Source Files + @@ -220,5 +223,8 @@ Header Files + + Header Files + \ No newline at end of file