00001 //===================================================== 00002 // File : STL_Timer.hh 00003 // Author : L. Plagne <laurent.plagne@edf.fr)> 00004 // Copyright (C) EDF R&D, mar déc 3 18:59:35 CET 2002 00005 //===================================================== 00006 // 00007 // This program is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 00010 // of the License, or (at your option) any later version. 00011 // 00012 // This program is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 // 00020 // STL Timer Class. Adapted (L.P.) from the timer class by Musser et Al 00021 // described int the Book : STL Tutorial and reference guide. 00022 // Define a timer class for analyzing algorithm performance. 00023 #include <iostream> 00024 #include <iomanip> 00025 #include <vector> 00026 #include <map> 00027 #include <algorithm> 00028 using namespace std; 00029 00030 class STL_Timer { 00031 public: 00032 STL_Timer(){ baseline = false; }; // Default constructor 00033 // Start a series of r trials: 00034 void start(unsigned int r){ 00035 reps = r; 00036 count = 0; 00037 iterations.clear(); 00038 iterations.reserve(reps); 00039 initial = time(0); 00040 }; 00041 // Start a series of r trials to determine baseline time: 00042 void start_baseline(unsigned int r) 00043 { 00044 baseline = true; 00045 start(r); 00046 } 00047 // Returns true if the trials have been completed, else false 00048 bool check() 00049 { 00050 ++count; 00051 final = time(0); 00052 if (initial < final) { 00053 iterations.push_back(count); 00054 initial = final; 00055 count = 0; 00056 } 00057 return (iterations.size() < reps); 00058 }; 00059 // Returns the results for external use 00060 double get_time( void ) 00061 { 00062 sort(iterations.begin(), iterations.end()); 00063 return 1.0/iterations[reps/2]; 00064 }; 00065 private: 00066 unsigned int reps; // Number of trials 00067 // For storing loop iterations of a trial 00068 vector<long> iterations; 00069 // For saving initial and final times of a trial 00070 time_t initial, final; 00071 // For counting loop iterations of a trial 00072 unsigned long count; 00073 // true if this is a baseline computation, false otherwise 00074 bool baseline; 00075 // For recording the baseline time 00076 double baseline_time; 00077 }; 00078