43 void wallTimer::start()
45 void wallTimer::stop()
47 double wallTimer::acc()
const 50 int wallTimerSet::getIndex(
const string &name)
52 maptype::const_iterator it = lut.find(name);
55 timer.push_back(wallTimer());
56 lut[name]=timer.size()-1;
57 return timer.size()-1;
60 void wallTimerSet::start(
int index)
61 { timer[index].start(); }
62 void wallTimerSet::stop(
int index)
63 { timer[index].stop(); }
64 void wallTimerSet::stopstart(
int index1,
int index2)
65 {
double t=
wallTime(); timer[index1].stop(t); timer[index2].start(t); }
66 void wallTimerSet::reset(
int index)
67 { timer[index].reset(); }
68 double wallTimerSet::acc(
int index)
69 {
return timer[index].acc(); }
70 void wallTimerSet::start(
const string &name)
71 { start(getIndex(name)); }
72 void wallTimerSet::stop(
const string &name)
73 { stop(getIndex(name)); }
74 void wallTimerSet::stopstart(
const string &name1,
const string &name2)
75 { stopstart(getIndex(name1),getIndex(name2)); }
76 void wallTimerSet::reset(
const string &name)
77 { reset(getIndex(name)); }
78 double wallTimerSet::acc(
const string &name)
79 {
return acc(getIndex(name)); }
81 void wallTimerSet::report()
const 83 cout <<
"\nWall clock timer report:" << endl;
84 for (maptype::const_iterator it=lut.begin(); it!=lut.end(); ++it)
85 printf(
" %-15s: %10.5fs\n", it->first.c_str(), timer[it->second].acc());
86 cout <<
"End wall clock timer report\n" << endl;
89 wallTimerSet wallTimers;
95 typedef map<string,tstack_node>::iterator Ti;
96 typedef map<string,tstack_node>::const_iterator Tci;
97 typedef pair<Tci,double> Tipair;
105 map<string,tstack_node> child;
107 tstack_node(
const string &name_, tstack_node *parent_)
108 : parent(parent_), name(name_) {}
110 int max_namelen()
const 112 int res=name.length();
113 for (Tci it=child.begin(); it!=child.end(); ++it)
114 res=max(res,it->second.max_namelen());
119 tstack_node tstack_root(
"root",0);
120 tstack_node *curnode=0;
125 bool operator() (
const Tipair &a,
const Tipair &b)
const 126 {
return a.second>b.second; }
129 void tstack_report(
const tstack_node &node,
const string &indent,
int twidth,
132 double total=node.wt.acc();
134 for (Tci it=node.child.begin(); it!=node.child.end(); ++it)
135 tmp.push_back(make_pair(it,it->second.wt.acc()));
139 sort(tmp.begin(),tmp.end(),timecomp());
141 printf(
"%s|\n", indent.c_str());
142 for (
unsigned i=0; i<tmp.size(); ++i)
144 printf(
"%s+- %-*s:%6.2f%% (%*.4fs)\n",indent.c_str(),slen,
145 (tmp[i].first->first).c_str(), 100*tmp[i].second/total,twidth,
147 tstack_report(tmp[i].first->second,indent+
"| ",twidth,slen);
150 printf(
"%s+- %-*s:%6.2f%% (%*.4fs)\n%s\n",indent.c_str(),slen,
151 "<unaccounted>",100*(total-tsum)/total,twidth,total-tsum,indent.c_str());
157 void tstack_push(
const string &name)
160 if (curnode==0) curnode=&tstack_root;
161 Ti it=curnode->child.find(name);
162 if (it==curnode->child.end())
163 it=curnode->child.insert (make_pair(name,tstack_node(name,curnode))).first;
164 curnode=&(it->second);
166 curnode->wt.start(0.5*(t0+t1));
169 void tstack_pop(
const string &name)
172 planck_assert(curnode && (curnode->name==name),
"invalid tstack operation");
174 curnode->wt.stop(0.5*(t0+t1));
175 curnode=curnode->parent;
183 curnode->wt.stop(0.5*(t0+t1));
184 curnode=curnode->parent;
187 void tstack_replace(
const string &name2)
191 tstack_node *savenode=curnode;
192 curnode=curnode->parent;
193 Ti it=curnode->child.find(name2);
194 if (it==curnode->child.end())
195 it=curnode->child.insert(make_pair(name2,tstack_node(name2,curnode))).first;
196 curnode=&(it->second);
198 double t=0.5*(t0+t1);
199 savenode->wt.stop(t);
200 curnode->wt.start(t);
203 void tstack_replace(
const string &name1,
const string &name2)
205 planck_assert(curnode && (curnode->name==name1),
"invalid tstack operation");
206 tstack_replace(name2);
209 void tstack_report(
const string &stem)
211 const tstack_node *ptr = 0;
212 for (Tci it=tstack_root.child.begin(); it!=tstack_root.child.end(); ++it)
213 if (it->first==stem) ptr=&(it->second);
215 int slen=string(
"<unaccounted>").size();
216 slen = max(slen,ptr->max_namelen());
218 double total=ptr->wt.acc();
219 printf(
"\nTotal wall clock time for '%s': %1.4fs\n",stem.c_str(),total);
221 int logtime=max(1,
int(log10(total)+1));
222 tstack_report(*ptr,
"",logtime+5,slen);
224 printf(
"\nAccumulated timing overhead: approx. %1.4fs\n",overhead);
#define planck_assert(testval, msg)