-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock_watch.cpp
More file actions
165 lines (135 loc) · 3.02 KB
/
Copy pathclock_watch.cpp
File metadata and controls
165 lines (135 loc) · 3.02 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <vector>
#include <cmath>
#include <chrono>
using namespace std::chrono;
class TimerBase
{
private:
system_clock::time_point m_start;
public:
TimerBase() : m_start(system_clock::time_point::min()) {}
void Clear()
{
m_start = system_clock::time_point::min();
}
bool IsStarted() const
{
return (m_start.time_since_epoch() != system_clock::duration(0));
}
void Start()
{
m_start = system_clock::now();
}
unsigned long GetMs()
{
if(IsStarted())
{
system_clock::duration diff;
diff = system_clock::now() - m_start;
return (unsigned)(duration_cast<milliseconds>(diff).count());
}
return 0;
}
};
class TimerBaseClock
{
private:
clock_t m_start;
public:
TimerBaseClock()
{
m_start = -1;
}
void Clear();
bool IsStarted() const
{
return (m_start != -1);
}
void Start()
{
m_start = clock();
}
unsigned long GetMs()
{
clock_t now;
if(IsStarted())
{
now = clock();
clock_t dt = (now-m_start);
return (unsigned long)(dt*1000/ CLOCKS_PER_SEC);
}
return 0;
}
};
class TimerBaseChrono
{
private:
std::chrono::system_clock::time_point m_start;
public:
TimerBaseChrono() : m_start(system_clock::time_point::min())
{
}
void Clear()
{
m_start = system_clock::time_point::min();
}
bool IsStarted() const
{
return (m_start != system_clock::time_point::min());
}
void Start()
{
m_start = std::chrono::system_clock::now();
}
unsigned long GetMs()
{
if(IsStarted())
{
system_clock::duration diff;
diff = system_clock::now() - m_start;
return (unsigned)(duration_cast<illiseconds>(diff).count());
}
return 0;
}
};
template <typename T> class basic_stopwatch : T
{
typedef typename T BaseTimer;
public:
explicit basic_stopwatch(bool start);
explicit basic_stopwatch(char const* activity = "stopwatch", bool start = true);
basic_stopwatch(std::ostream& log, char const* activitiy = "stopwatch", bool start = true);
~basic_stopwatch();
unsigned LapGet() const;
bool IsStarted() const;
unsigned Show(char const* event = "show");
unsigned Start(char const* event_name = "start");
unsigned Stop(char const* event_name = "stop");
private:
char const* m_activity;
unsigned m_lap;
std::ostream& m_log;
};
// Test Hanes //
counter_t const iterations = 10000; // 64bit !
...
{
Stopwatch sw("function to be time()");
for(counter_t i = 0; i<iterations ;++i)
{
result = function_to_be_timed();
}
}
// False While Loop //
do
{
if (!operation1())
{
break;
}
if(!operation2(x,y,z))
{
break;
}
} while(0); // exit !