Skip to content

Commit d417256

Browse files
committed
Move timer code to own cpp/h files.
1 parent 9a52b35 commit d417256

File tree

12 files changed

+233
-132
lines changed

12 files changed

+233
-132
lines changed

cli/cppcheck.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@
272272
RelativePath="threadexecutor.cpp"
273273
>
274274
</File>
275+
<File
276+
RelativePath="..\lib\timer.cpp"
277+
>
278+
</File>
275279
<File
276280
RelativePath="..\lib\token.cpp"
277281
>
@@ -370,6 +374,10 @@
370374
RelativePath="..\lib\settings.h"
371375
>
372376
</File>
377+
<File
378+
RelativePath="..\lib\timer.h"
379+
>
380+
</File>
373381
<File
374382
RelativePath="..\lib\token.h"
375383
>

cli/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
<ClCompile Include="..\lib\path.cpp" />
141141
<ClCompile Include="..\lib\preprocessor.cpp" />
142142
<ClCompile Include="..\lib\settings.cpp" />
143+
<ClCompile Include="..\lib\timer.cpp" />
143144
<ClCompile Include="..\lib\token.cpp" />
144145
<ClCompile Include="..\lib\tokenize.cpp" />
145146
<ClCompile Include="cppcheckexecutor.cpp" />
@@ -166,6 +167,7 @@
166167
<ClInclude Include="..\lib\path.h" />
167168
<ClInclude Include="..\lib\preprocessor.h" />
168169
<ClInclude Include="..\lib\settings.h" />
170+
<ClInclude Include="..\lib\timer.h" />
169171
<ClInclude Include="..\lib\token.h" />
170172
<ClInclude Include="..\lib\tokenize.h" />
171173
<ClInclude Include="resource.h" />

cli/cppcheck.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
<ClCompile Include="..\lib\path.cpp">
7878
<Filter>Source Files</Filter>
7979
</ClCompile>
80+
<ClCompile Include="..\lib\timer.cpp">
81+
<Filter>Source Files</Filter>
82+
</ClCompile>
8083
</ItemGroup>
8184
<ItemGroup>
8285
<ClInclude Include="resource.h">
@@ -145,6 +148,9 @@
145148
<ClInclude Include="..\lib\path.h">
146149
<Filter>Header Files</Filter>
147150
</ClInclude>
151+
<ClInclude Include="..\lib\timer.h">
152+
<Filter>Header Files</Filter>
153+
</ClInclude>
148154
</ItemGroup>
149155
<ItemGroup>
150156
<ResourceCompile Include="cppcheck.rc" />

lib/cppcheck.cpp

Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -32,141 +32,10 @@
3232
#include <fstream>
3333
#include <stdexcept>
3434
#include <ctime>
35-
36-
/*
37-
TODO:
38-
- handle SHOWTIME_TOP5 in TimerResults
39-
- sort list by time
40-
- do not sort the results alphabetically
41-
- rename "file" to "single"
42-
- synchronise map access in multithreaded mode or disable timing
43-
- add unit tests
44-
- for --showtime (needs input file)
45-
- for Timer* classes
46-
- move timer stuff to seperate source/header
47-
*/
48-
enum
49-
{
50-
SHOWTIME_NONE = 0,
51-
SHOWTIME_FILE,
52-
SHOWTIME_SUMMARY,
53-
SHOWTIME_TOP5
54-
};
55-
56-
class TimerResultsIntf
57-
{
58-
public:
59-
virtual ~TimerResultsIntf() { }
60-
61-
virtual void AddResults(const std::string& str, std::clock_t clocks) = 0;
62-
};
63-
64-
struct TimerResultsData
65-
{
66-
std::clock_t _clocks;
67-
long _numberOfResults;
68-
69-
TimerResultsData()
70-
: _clocks(0)
71-
, _numberOfResults(0)
72-
{
73-
}
74-
};
75-
76-
class TimerResults : public TimerResultsIntf
77-
{
78-
public:
79-
TimerResults()
80-
{
81-
}
82-
83-
void ShowResults()
84-
{
85-
std::clock_t overallClocks = 0;
86-
87-
std::map<std::string, struct TimerResultsData>::const_iterator I = _results.begin();
88-
const std::map<std::string, struct TimerResultsData>::const_iterator E = _results.end();
89-
90-
while (I != E)
91-
{
92-
const double sec = (double)I->second._clocks / CLOCKS_PER_SEC;
93-
const double secAverage = (double)(I->second._clocks / I->second._numberOfResults) / CLOCKS_PER_SEC;
94-
std::cout << I->first << ": " << sec << "s (avg. " << secAverage << "s - " << I->second._numberOfResults << " result(s))" << std::endl;
95-
96-
overallClocks += I->second._clocks;
97-
98-
++I;
99-
}
100-
101-
const double secOverall = (double)overallClocks / CLOCKS_PER_SEC;
102-
std::cout << "Overall time: " << secOverall << "s" << std::endl;
103-
}
104-
105-
virtual void AddResults(const std::string& str, std::clock_t clocks)
106-
{
107-
_results[str]._clocks += clocks;
108-
_results[str]._numberOfResults++;
109-
}
110-
111-
private:
112-
std::map<std::string, struct TimerResultsData> _results;
113-
};
35+
#include "timer.h"
11436

11537
static TimerResults S_timerResults;
11638

117-
class Timer
118-
{
119-
public:
120-
Timer(const std::string& str, unsigned int showtimeMode, TimerResultsIntf* timerResults = NULL)
121-
: _str(str)
122-
, _showtimeMode(showtimeMode)
123-
, _start(0)
124-
, _stopped(false)
125-
, _timerResults(timerResults)
126-
{
127-
if (showtimeMode != SHOWTIME_NONE)
128-
_start = std::clock();
129-
}
130-
131-
~Timer()
132-
{
133-
Stop();
134-
}
135-
136-
void Stop()
137-
{
138-
if ((_showtimeMode != SHOWTIME_NONE) && !_stopped)
139-
{
140-
const std::clock_t end = std::clock();
141-
const std::clock_t diff = end - _start;
142-
143-
if (_showtimeMode == SHOWTIME_FILE)
144-
{
145-
double sec = (double)diff / CLOCKS_PER_SEC;
146-
std::cout << _str << ": " << sec << "s" << std::endl;
147-
}
148-
else
149-
{
150-
if (_timerResults)
151-
_timerResults->AddResults(_str, diff);
152-
}
153-
}
154-
155-
_stopped = true;
156-
}
157-
158-
private:
159-
Timer& operator=(const Timer&); // disallow assignments
160-
161-
const std::string _str;
162-
const unsigned int _showtimeMode;
163-
std::clock_t _start;
164-
bool _stopped;
165-
TimerResultsIntf* _timerResults;
166-
};
167-
168-
//---------------------------------------------------------------------------
169-
17039
CppCheck::CppCheck(ErrorLogger &errorLogger)
17140
: _errorLogger(errorLogger)
17241
{

lib/lib.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@
222222
RelativePath=".\settings.cpp"
223223
>
224224
</File>
225+
<File
226+
RelativePath=".\timer.cpp"
227+
>
228+
</File>
225229
<File
226230
RelativePath=".\token.cpp"
227231
>
@@ -316,6 +320,10 @@
316320
RelativePath=".\settings.h"
317321
>
318322
</File>
323+
<File
324+
RelativePath=".\timer.h"
325+
>
326+
</File>
319327
<File
320328
RelativePath=".\token.h"
321329
>

lib/lib.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<ClCompile Include="path.cpp" />
103103
<ClCompile Include="preprocessor.cpp" />
104104
<ClCompile Include="settings.cpp" />
105+
<ClCompile Include="timer.cpp" />
105106
<ClCompile Include="token.cpp" />
106107
<ClCompile Include="tokenize.cpp" />
107108
</ItemGroup>
@@ -125,6 +126,7 @@
125126
<ClInclude Include="path.h" />
126127
<ClInclude Include="preprocessor.h" />
127128
<ClInclude Include="settings.h" />
129+
<ClInclude Include="timer.h" />
128130
<ClInclude Include="token.h" />
129131
<ClInclude Include="tokenize.h" />
130132
</ItemGroup>

lib/lib.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
<ClCompile Include="path.cpp">
6969
<Filter>Source Files</Filter>
7070
</ClCompile>
71+
<ClCompile Include="timer.cpp">
72+
<Filter>Source Files</Filter>
73+
</ClCompile>
7174
</ItemGroup>
7275
<ItemGroup>
7376
<ClInclude Include="check.h">
@@ -133,5 +136,8 @@
133136
<ClInclude Include="path.h">
134137
<Filter>Header Files</Filter>
135138
</ClInclude>
139+
<ClInclude Include="timer.h">
140+
<Filter>Header Files</Filter>
141+
</ClInclude>
136142
</ItemGroup>
137143
</Project>

lib/timer.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#include <iostream>
19+
#include "timer.h"
20+
21+
/*
22+
TODO:
23+
- handle SHOWTIME_TOP5 in TimerResults
24+
- sort list by time
25+
- do not sort the results alphabetically
26+
- rename "file" to "single"
27+
- synchronise map access in multithreaded mode or disable timing
28+
- add unit tests
29+
- for --showtime (needs input file)
30+
- for Timer* classes
31+
*/
32+
33+
34+
void TimerResults::ShowResults()
35+
{
36+
std::clock_t overallClocks = 0;
37+
38+
std::map<std::string, struct TimerResultsData>::const_iterator I = _results.begin();
39+
const std::map<std::string, struct TimerResultsData>::const_iterator E = _results.end();
40+
41+
while (I != E)
42+
{
43+
const double sec = (double)I->second._clocks / CLOCKS_PER_SEC;
44+
const double secAverage = (double)(I->second._clocks / I->second._numberOfResults) / CLOCKS_PER_SEC;
45+
std::cout << I->first << ": " << sec << "s (avg. " << secAverage << "s - " << I->second._numberOfResults << " result(s))" << std::endl;
46+
47+
overallClocks += I->second._clocks;
48+
49+
++I;
50+
}
51+
52+
const double secOverall = (double)overallClocks / CLOCKS_PER_SEC;
53+
std::cout << "Overall time: " << secOverall << "s" << std::endl;
54+
}
55+
56+
void TimerResults::AddResults(const std::string& str, std::clock_t clocks)
57+
{
58+
_results[str]._clocks += clocks;
59+
_results[str]._numberOfResults++;
60+
}
61+
62+
Timer::Timer(const std::string& str, unsigned int showtimeMode, TimerResultsIntf* timerResults)
63+
: _str(str)
64+
, _showtimeMode(showtimeMode)
65+
, _start(0)
66+
, _stopped(false)
67+
, _timerResults(timerResults)
68+
{
69+
if (showtimeMode != SHOWTIME_NONE)
70+
_start = std::clock();
71+
}
72+
73+
Timer::~Timer()
74+
{
75+
Stop();
76+
}
77+
78+
void Timer::Stop()
79+
{
80+
if ((_showtimeMode != SHOWTIME_NONE) && !_stopped)
81+
{
82+
const std::clock_t end = std::clock();
83+
const std::clock_t diff = end - _start;
84+
85+
if (_showtimeMode == SHOWTIME_FILE)
86+
{
87+
double sec = (double)diff / CLOCKS_PER_SEC;
88+
std::cout << _str << ": " << sec << "s" << std::endl;
89+
}
90+
else
91+
{
92+
if (_timerResults)
93+
_timerResults->AddResults(_str, diff);
94+
}
95+
}
96+
97+
_stopped = true;
98+
}

0 commit comments

Comments
 (0)