Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Profiler.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2001 Yoshua Bengio and University of Montreal 00005 // 00006 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: Profiler.cc,v 1.2 2004/02/20 21:11:49 chrish42 Exp $ 00037 * This file is part of the PLearn library. 00038 ******************************************************* */ 00039 00040 #include "Profiler.h" 00041 #include <time.h> 00042 namespace PLearn { 00043 using namespace std; 00044 00045 // initialize static variables 00046 map<string,Profiler::Stats> Profiler::codes_statistics; 00047 struct tms Profiler::t; 00048 bool Profiler::active = false; 00049 00050 #ifdef PROFILE 00051 // start recording time for named piece of code 00052 void Profiler::start(const string& name_of_piece_of_code) 00053 { 00054 if (active) 00055 { 00056 map<string,Profiler::Stats>::iterator it = 00057 codes_statistics.find(name_of_piece_of_code); 00058 if (it == codes_statistics.end()) 00059 { 00060 Stats stats; 00061 stats.on_going = true; 00062 stats.time_of_last_start = times(&t); 00063 codes_statistics[name_of_piece_of_code] = stats; 00064 } 00065 else 00066 { 00067 Profiler::Stats& stats = it->second; 00068 if (stats.on_going) 00069 PLERROR("Profiler::start(%s) called while previous start had not ended", 00070 name_of_piece_of_code.c_str()); 00071 stats.on_going = true; 00072 stats.time_of_last_start = times(&t); 00073 } 00074 } 00075 } 00076 00077 00078 00079 // end recording time for named piece of code, and increment 00080 // frequency of occurence and total duration of this piece of code. 00081 void Profiler::end(const string& name_of_piece_of_code) 00082 { 00083 if (active) 00084 { 00085 clock_t end_time = times(&t); 00086 map<string,Profiler::Stats>::iterator it = 00087 codes_statistics.find(name_of_piece_of_code); 00088 if (it == codes_statistics.end()) 00089 PLERROR("Profiler::end(%s) called before any call to start(%s)", 00090 name_of_piece_of_code.c_str(),name_of_piece_of_code.c_str()); 00091 Profiler::Stats& stats = it->second; 00092 if (!stats.on_going) 00093 PLERROR("Profiler::end(%s) called before previous start was called", 00094 name_of_piece_of_code.c_str()); 00095 stats.on_going = false; 00096 stats.frequency_of_occurence++; 00097 int duration = end_time - stats.time_of_last_start; 00098 if (end_time < stats.time_of_last_start) 00099 { duration=1; PLWARNING("Profiler: negative duration measured with times!"); } 00100 stats.total_duration += duration; 00101 } 00102 } 00103 #endif 00104 00105 // output a report on the output stream, giving 00106 // the statistics recorded for each of the named pieces of codes. 00107 void Profiler::report(ostream& out) 00108 { 00109 map<string,Profiler::Stats>::iterator it = 00110 codes_statistics.begin(), end = codes_statistics.end(); 00111 00112 for (;it!=end; ++it) 00113 { 00114 out << " Ticks per second : " << sysconf(_SC_CLK_TCK)<<endl; 00115 out << "For " << it->first << " :" << endl; 00116 Profiler::Stats& stats = it->second; 00117 out << "Frequency of occurence = " << stats.frequency_of_occurence << endl; 00118 out << "Total duration = " << stats.total_duration << endl; 00119 double avg_duration = (double)stats.total_duration/stats.frequency_of_occurence; 00120 out << "Average duration = " << avg_duration << endl; 00121 out << endl; 00122 } 00123 } 00124 00125 00126 00127 } // end of namespace PLearn

Generated on Tue Aug 17 16:03:15 2004 for PLearn by doxygen 1.3.7