00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
#include "Profiler.h"
00041
#include <time.h>
00042
namespace PLearn {
00043
using namespace std;
00044
00045
00046 map<string,Profiler::Stats> Profiler::codes_statistics;
00047
struct tms Profiler::t;
00048 bool Profiler::active = false;
00049
00050 #ifdef
PROFILE
00051
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
00080
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
00106
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 }