-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathTimer.cpp
149 lines (117 loc) · 2.88 KB
/
Timer.cpp
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
145
146
147
148
149
/*
DDS, a bridge double dummy solver.
Copyright (C) 2006-2014 by Bo Haglund /
2014-2018 by Bo Haglund & Soren Hein.
See LICENSE and README.
*/
#include <iostream>
#include <iomanip>
#include <sstream>
#include "Timer.h"
using std::chrono::duration_cast;
using std::chrono::microseconds;
Timer::Timer()
{
Timer::Reset();
}
void Timer::Reset()
{
name = "";
count = 0;
userCum = 0;
systCum = 0;
}
void Timer::SetName(const string& nameIn)
{
name = nameIn;
}
void Timer::Start()
{
user0 = Clock::now();
syst0 = clock();
}
void Timer::End()
{
time_point<Clock> user1 = Clock::now();
clock_t syst1 = clock();
chrono::duration<double, micro> d = user1 - user0;
int tuser = static_cast<int>(d.count());
count++;
userCum += tuser;
systCum += static_cast<long>(syst1) -
static_cast<long>(syst0);
}
bool Timer::Used() const
{
return (count > 0);
}
int Timer::UserTime() const
{
return static_cast<int>(userCum);
}
void Timer::operator +=(const Timer& add)
{
count += add.count;
userCum += add.userCum;
systCum += add.systCum;
}
void Timer::operator -=(const Timer& deduct)
{
if (deduct.userCum > userCum)
userCum = 0;
else
userCum -= deduct.userCum;
if (deduct.systCum > systCum)
systCum = 0;
else
systCum -= deduct.systCum;
}
string Timer::SumLine(
const Timer& divisor,
const string& bname) const
{
stringstream ss;
if (count > 0)
{
ss << setw(14) << left << (bname == "" ? name : bname) <<
setw(9) << right << count <<
setw(11) << userCum <<
setw(7) << setprecision(2) << fixed <<
userCum / static_cast<double>(count) <<
setw(5) << setprecision(1) << fixed <<
100. * userCum / divisor.userCum <<
setw(11) << setprecision(0) << fixed <<
1000000 * systCum / static_cast<double>(CLOCKS_PER_SEC) <<
setw(7) << setprecision(2) << fixed <<
1000000 * systCum / static_cast<double>(count * CLOCKS_PER_SEC) <<
setw(5) << setprecision(1) << fixed <<
100. * systCum / divisor.systCum << "\n";
}
else
{
ss << setw(14) << left << (bname == "" ? name : bname) <<
setw(9) << right << count <<
setw(11) << userCum <<
setw(7) << "-" <<
setw(5) << "-" <<
setw(11) << 1000000 * systCum / static_cast<double>(CLOCKS_PER_SEC) <<
setw(7) << "-" <<
setw(5) << "-" << "\n";
}
return ss.str();
}
string Timer::DetailLine() const
{
stringstream ss;
ss << setw(15) << left << name <<
setw(10) << right << count <<
setw(11) << right << userCum <<
setw(11) << setprecision(2) << fixed <<
userCum / static_cast<double>(count) <<
setw(11) << setprecision(0) << fixed <<
1000000 * systCum / static_cast<double>(CLOCKS_PER_SEC) <<
setw(11) << setprecision(2) << fixed <<
1000000 * systCum /
static_cast<double>(count * CLOCKS_PER_SEC) << "\n";
return ss.str();
}