1 | //=====================================================
|
---|
2 | // File : STL_Timer.hh
|
---|
3 | // Author : L. Plagne <laurent.plagne@edf.fr)>
|
---|
4 | // Copyright (C) EDF R&D, mar déc 3 18:59:35 CET 2002
|
---|
5 | //=====================================================
|
---|
6 | //
|
---|
7 | // This program is free software; you can redistribute it and/or
|
---|
8 | // modify it under the terms of the GNU General Public License
|
---|
9 | // as published by the Free Software Foundation; either version 2
|
---|
10 | // of the License, or (at your option) any later version.
|
---|
11 | //
|
---|
12 | // This program is distributed in the hope that it will be useful,
|
---|
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | // GNU General Public License for more details.
|
---|
16 | // You should have received a copy of the GNU General Public License
|
---|
17 | // along with this program; if not, write to the Free Software
|
---|
18 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
19 | //
|
---|
20 | // STL Timer Class. Adapted (L.P.) from the timer class by Musser et Al
|
---|
21 | // described int the Book : STL Tutorial and reference guide.
|
---|
22 | // Define a timer class for analyzing algorithm performance.
|
---|
23 | #include <iostream>
|
---|
24 | #include <iomanip>
|
---|
25 | #include <vector>
|
---|
26 | #include <map>
|
---|
27 | #include <algorithm>
|
---|
28 | using namespace std;
|
---|
29 |
|
---|
30 | class STL_Timer {
|
---|
31 | public:
|
---|
32 | STL_Timer(){ baseline = false; }; // Default constructor
|
---|
33 | // Start a series of r trials:
|
---|
34 | void start(unsigned int r){
|
---|
35 | reps = r;
|
---|
36 | count = 0;
|
---|
37 | iterations.clear();
|
---|
38 | iterations.reserve(reps);
|
---|
39 | initial = time(0);
|
---|
40 | };
|
---|
41 | // Start a series of r trials to determine baseline time:
|
---|
42 | void start_baseline(unsigned int r)
|
---|
43 | {
|
---|
44 | baseline = true;
|
---|
45 | start(r);
|
---|
46 | }
|
---|
47 | // Returns true if the trials have been completed, else false
|
---|
48 | bool check()
|
---|
49 | {
|
---|
50 | ++count;
|
---|
51 | final = time(0);
|
---|
52 | if (initial < final) {
|
---|
53 | iterations.push_back(count);
|
---|
54 | initial = final;
|
---|
55 | count = 0;
|
---|
56 | }
|
---|
57 | return (iterations.size() < reps);
|
---|
58 | };
|
---|
59 | // Returns the results for external use
|
---|
60 | double get_time( void )
|
---|
61 | {
|
---|
62 | sort(iterations.begin(), iterations.end());
|
---|
63 | return 1.0/iterations[reps/2];
|
---|
64 | };
|
---|
65 | private:
|
---|
66 | unsigned int reps; // Number of trials
|
---|
67 | // For storing loop iterations of a trial
|
---|
68 | vector<long> iterations;
|
---|
69 | // For saving initial and final times of a trial
|
---|
70 | time_t initial, final;
|
---|
71 | // For counting loop iterations of a trial
|
---|
72 | unsigned long count;
|
---|
73 | // true if this is a baseline computation, false otherwise
|
---|
74 | bool baseline;
|
---|
75 | // For recording the baseline time
|
---|
76 | double baseline_time;
|
---|
77 | };
|
---|
78 |
|
---|