-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimer.cpp
More file actions
110 lines (98 loc) · 2.3 KB
/
Timer.cpp
File metadata and controls
110 lines (98 loc) · 2.3 KB
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
#include "Timer.h"
#include <sstream>
using namespace std;
Timer Timer::s_ti;
Timer::tim::~tim()
{
map<string,tim*>::iterator it=m_timtree.begin();
while(it!=m_timtree.end()){
delete it->second;
it++;
}
}
void Timer::tic(string timer)
{
size_t p=timer.find("/");
tim* t=&s_ti.m_timers[timer.substr(0,p)];
if(p!=string::npos)
t=t->get_timer(timer.substr(p+1));
#ifdef USEPARA
t->m_t0=omp_get_wtime();
#else
t->m_t0=clock();
#endif
}
double Timer::toc(string timer)
{
size_t p=timer.find("/");
tim* t=&s_ti.m_timers[timer.substr(0,p)];
if(p!=string::npos)
t=t->get_timer(timer.substr(p+1));
#ifdef USEPARA
t->m_t+=omp_get_wtime()-t->m_t0;
#else
t->m_t+=(clock()-t->m_t0)/double(CLOCKS_PER_SEC);
#endif
return t->m_t;
}
const Timer& Timer::timer()
{
return s_ti;
}
double Timer::timer(std::string ti)
{
size_t p=ti.find("/");
if(p==string::npos)
return s_ti.m_timers[ti].m_t;
else
return s_ti.m_timers[ti.substr(0,p)].get_timer(ti.substr(p+1))->m_t;
}
void Timer::reset(std::string ti)
{
s_ti.m_timers[ti].m_t=0;
}
Timer::tim* Timer::tim::get_timer(string timer)
{
size_t p=timer.find("/");
if(p==string::npos){
if(m_timtree.find(timer)==m_timtree.end())
m_timtree[timer]=new tim;
return m_timtree[timer];
} else {
if(m_timtree.find(timer.substr(0,p))==m_timtree.end())
m_timtree[timer.substr(0,p)]=new tim;
return m_timtree[timer.substr(0,p)]->get_timer(timer.substr(p+1));
}
}
string Timer::report()
{
ostringstream strout;
map<string,tim>::iterator it=s_ti.m_timers.begin();
while(it!=s_ti.m_timers.end())
{
strout<<it->first<<": "<<it->second.report()<<endl;
++it;
}
return strout.str();
}
string Timer::tim::report()
{
ostringstream strout;
strout<<"total: "<<m_t<<", self: "<<get_self_time()<<endl;
map<string,tim*>::iterator it=m_timtree.begin();
while(it!=m_timtree.end()){
strout<<it->first<<": "<<it->second->report();
it++;
}
return strout.str();
}
double Timer::tim::get_self_time()
{
double out=m_t;
map<string,tim*>::iterator it=m_timtree.begin();
while(it!=m_timtree.end()){
out-=it->second->m_t;
it++;
}
return out;
}