-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclock_survey.html
221 lines (190 loc) · 5.95 KB
/
clock_survey.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Clcok Survey</title>
<style>
p {text-align:justify}
li {text-align:justify}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
ins {color:#00A000}
del {color:#A00000}
code {white-space:pre;}
</style>
</head>
<body>
<address align=right>
<br/>
<br/>
<a href="mailto:howard.hinnant@gmail.com">Howard E. Hinnant</a><br/>
2016-07-09<br/>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"> <img alt="Creative
Commons License" style="border-width:0"
src="http://i.creativecommons.org/l/by/4.0/80x15.png" /></a><br /> This work is licensed
under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative
Commons Attribution 4.0 International License</a>.
</address>
<hr/>
<h1 align=center><code><chrono></code> Clock Survey</h1>
<p>
This survey shows you how to discover the properties of the three
<code><chrono></code> clocks:
</p>
<ul>
<li><code>std::chrono::system_clock</code></li>
<li><code>std::chrono::steady_clock</code></li>
<li><code>std::chrono::high_resolution_clock</code></li>
</ul>
<p>
Here is code to discover each clock's properties:
</p>
<blockquote><pre>
#include <climits>
#include <chrono>
#include <cstddef>
#include <cstdlib>
#ifndef _MSC_VER
# include <cxxabi.h>
#endif
#include <iostream>
#include <memory>
#include <string>
#include <typeinfo>
#include <type_traits>
template <typename T>
std::string
type_name()
{
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void(*)(void*)> own
(
#ifndef _MSC_VER
abi::__cxa_demangle(typeid(TR).name(), nullptr,
nullptr, nullptr),
#else
nullptr,
#endif
std::free
);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += " const";
if (std::is_volatile<TR>::value)
r += " volatile";
if (std::is_lvalue_reference<T>::value)
r += "&";
else if (std::is_rvalue_reference<T>::value)
r += "&&";
return r;
}
template <class Clock>
void
stats(std::string const& msg)
{
std::cout << msg << '\n';
const std::size_t bits = CHAR_BIT * sizeof(typename Clock::rep);
std::cout << " rep is " << type_name<typename Clock::rep>() << " : "
<< bits << " bits\n";
std::cout << " period is " << Clock::period::num << '/'
<< Clock::period::den << '\n';
std::cout << " is_steady is " << Clock::is_steady << '\n';
}
int
main()
{
using namespace std::chrono;
stats<system_clock>("system_clock");
stats<steady_clock>("steady_clock");
stats<high_resolution_clock>("high_resolution_clock");
if (std::is_same<high_resolution_clock, system_clock>::value)
std::cout << "high_resolution_clock is the same type as system_clock\n";
if (std::is_same<high_resolution_clock, steady_clock>::value)
std::cout << "high_resolution_clock is the same type as steady_clock\n";
if (std::is_same<system_clock, steady_clock>::value)
std::cout << "system_clock is the same type as steady_clock\n";
}
</pre></blockquote>
<p>
There are three implementations of <code><chrono></code> that I am aware of:
</p>
<ul>
<li>LLVM's libc++</li>
<li>gcc's libstdc++</li>
<li>Visual Studio (VS)</li>
</ul>
<p>
The results are (I've taken minor liberities with the formatting):
</p>
<p><b>libc++:</b></p>
<blockquote><pre>
system_clock
rep is long long : 64 bits
period is 1/1,000,000
is_steady is 0
steady_clock
rep is long long : 64 bits
period is 1/1,000,000,000
is_steady is 1
high_resolution_clock
rep is long long : 64 bits
period is 1/1,000,000,000
is_steady is 1
high_resolution_clock is the same type as steady_clock
</pre></blockquote>
<p><b>libstdc++:</b></p>
<blockquote><pre>
system_clock
rep is long : 64 bits
period is 1/1,000,000,000
is_steady is 0
steady_clock
rep is long : 64 bits
period is 1/1,000,000,000
is_steady is 1
high_resolution_clock
rep is long : 64 bits
period is 1/1,000,000,000
is_steady is 0
high_resolution_clock is the same type as system_clock
</pre></blockquote>
<p><b>VS:</b></p>
<blockquote><pre>
system_clock
rep is __int64 : 64 bits
period is 1/10,000,000
is_steady is 0
steady_clock
rep is __int64 : 64 bits
period is 1/1,000,000,000
is_steady is 1
high_resolution_clock
rep is __int64 : 64 bits
period is 1/1,000,000,000
is_steady is 1
high_resolution_clock is the same type as steady_clock
</pre></blockquote>
<p>
There exist online compilers for each of these implementations, and so you can check
these results for yourself, on each of these three platforms.
</p>
<ul>
<li><a href="http://melpon.org/wandbox/permlink/uZFy3uMFXs7bLBQO">LLVM's libc++</a></li>
<li><a href="http://melpon.org/wandbox/permlink/qhXOxKN7t587uXgm">gcc's libstdc++</a></li>
<li><a href="http://webcompiler.cloudapp.net">Visual Studio (VS)</a></li>
</ul>
<p>
I have further found that each implementation of <code>std::chrono::system_clock</code>
is measuring <a href="https://en.wikipedia.org/wiki/Unix_time">Unix Time</a>. That is,
they count <i>non-leap-seconds</i> since 1970-01-01 00:00:00 UTC, except with a precision
as noted by the survey above. You can confirm this yourself with the software
<a href="http://howardhinnant.github.io/date_algorithms.html#How%20can%20I%20confirm%20that%20your%20assertions%20about%20<code>chrono</code>%20compatibility%20are%20correct?">at this link</a>.
</p>
</body>
</html>