Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Time_Tracer.h
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* CCOPYRIGHT */
#if !defined(Time_Tracer_h)
#define Time_Tracer_h
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <set>
namespace Utilities{
class TimingFunction
{
public:
TimingFunction(const char * pstr):
str(pstr),
time_taken(0.0),
times_called(0)
{}
class comparer
{
public:
bool operator()(const TimingFunction* t1, const TimingFunction* t2) const
{
return strcmp(t1->str, t2->str) < 0;
}
};
void start() {start_time = clock();}
void end() {time_taken += clock()-start_time; times_called++;}
friend comparer;
friend ostream& operator<<(ostream& ostr, const TimingFunction* t);
protected:
const char* str;
clock_t time_taken;
int times_called;
clock_t start_time;
private:
TimingFunction();
const TimingFunction& operator=(TimingFunction&);
TimingFunction(TimingFunction&);
};
inline ostream& operator<<(ostream& ostr, const TimingFunction* t)
{
ostr << "<tr><td>" << t->str;
ostr.setf(0, ios::floatfield);
ostr << "<td align=center>" << t->time_taken/CLOCKS_PER_SEC;
ostr.setf(ios::scientific, ios::floatfield);
ostr << "<td align=center>" << t->times_called << "<td align=center>" << (t->time_taken/t->times_called)/CLOCKS_PER_SEC;
ostr << "</tr>";
return ostr;
}
// Non Newmat Tracer:
class Time_Tracer
{
public:
Time_Tracer(char* str)
{
if(debug)
{
tmp = "";
pad++;
for(unsigned int i = 0; i < pad; i++)
tmp = tmp + " ";
cout << tmp << str << endl;
}
if(timingon)
{
// see if already in list:
timingFunction = new TimingFunction(str);
set<TimingFunction*, TimingFunction::comparer>::iterator it = timingFunctions.find(timingFunction);
if(it== timingFunctions.end())
{
timingFunctions.insert(timingFunction);
}
else
{
timingFunction = *it;
}
timingFunction->start();
}
}
virtual ~Time_Tracer()
{
if(debug && pad > 0)
{
cout << tmp << "finished" << endl;
pad--;
}
if(timingon)
{
timingFunction->end();
}
}
static void dump_times(const string& dir)
{
ofstream out;
out.open((dir + "/timings.html").c_str(), ios::out);
out << "<HTML><TITLE>Tracer Timings</TITLE><BODY><table border=3 cellspacing=5>" << endl;
out << "<tr><td>Function<td align=center>Total Time(secs)<td align=center>Num of calls<td align=center>Time per call(secs)</tr>" << endl;
copy(timingFunctions.begin(), timingFunctions.end(), ostream_iterator<TimingFunction*>(out, "\n"));
out << "</table></BODY></HTML>" << endl;
out.close();
}
static void setdebugon() {debug = true;}
static void settimingon() {timingon = true;}
protected:
static bool debug;
static bool timingon;
static unsigned int pad;
static set<TimingFunction*, TimingFunction::comparer> timingFunctions;
string tmp;
TimingFunction* timingFunction;
private:
Time_Tracer();
const Time_Tracer& operator=(Time_Tracer&);
Time_Tracer(Time_Tracer&);
};
}
#endif