1// test_system_clock.cpp ----------------------------------------------------------//
2
3// Copyright 2008 Howard Hinnant
4// Copyright 2008 Beman Dawes
5// Copyright 2009 Vicente J. Botet Escriba
6
7// Distributed under the Boost Software License, Version 1.0.
8// See http://www.boost.org/LICENSE_1_0.txt
9
10/*
11This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
12was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
13Many thanks to Howard for making his code available under the Boost license.
14The original code was modified to conform to Boost conventions and to section
1520.9 Time utilities [time] of the C++ committee's working paper N2798.
16See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
17
18time2_demo contained this comment:
19
20 Much thanks to Andrei Alexandrescu,
21 Walter Brown,
22 Peter Dimov,
23 Jeff Garland,
24 Terry Golubiewski,
25 Daniel Krugler,
26 Anthony Williams.
27*/
28
29#include <boost/chrono/chrono.hpp>
30#include <boost/type_traits.hpp>
31
32#include <iostream>
33
34#include "clock_name.hpp"
35
36#if defined(BOOST_NO_CXX11_CONSTEXPR)
37
38using namespace boost::chrono;
39
40template <typename Clock>
41void test_clock()
42{
43 std::cout << "\n"<< name<Clock>::apply() << " test" << std::endl;
44{
45 typename Clock::duration delay = milliseconds(5);
46 typename Clock::time_point start = Clock::now();
47 while (Clock::now() - start <= delay)
48 ;
49 typename Clock::time_point stop = Clock::now();
50 //typename Clock::duration elapsed = stop - start;
51 std::cout << "5 milliseconds paused " << nanoseconds(stop - start).count() << " nanoseconds\n";
52}
53{
54 typename Clock::time_point start = Clock::now();
55 typename Clock::time_point stop;
56 std::size_t counter=1;
57 while ((stop=Clock::now()) == start) {
58 ++counter;
59 }
60 //typename Clock::duration elapsed = stop - start;
61 std::cout << "After " << counter << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
62
63 start = Clock::now();
64 for (std::size_t c=counter; c>0; --c) {
65 stop=Clock::now();;
66 }
67 std::cout << "After " << counter << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
68
69
70}
71{
72 typename Clock::time_point start = Clock::now();
73 typename Clock::time_point stop = Clock::now();
74 std::cout << "Resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
75}
76}
77
78void test_system_clock()
79{
80 std::cout << "system_clock test" << std::endl;
81 system_clock::duration delay = milliseconds(5);
82 system_clock::time_point start = system_clock::now();
83 while (system_clock::now() - start <= delay)
84 ;
85 system_clock::time_point stop = system_clock::now();
86 system_clock::duration elapsed = stop - start;
87 std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
88 start = system_clock::now();
89 stop = system_clock::now();
90 std::cout << "system_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
91}
92
93void test_steady_clock()
94{
95#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
96 std::cout << "steady_clock test" << std::endl;
97 steady_clock::duration delay = milliseconds(5);
98 steady_clock::time_point start = steady_clock::now();
99 while (steady_clock::now() - start <= delay)
100 ;
101 steady_clock::time_point stop = steady_clock::now();
102 steady_clock::duration elapsed = stop - start;
103 std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
104 start = steady_clock::now();
105 stop = steady_clock::now();
106 std::cout << "steady_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
107#endif
108}
109void test_hi_resolution_clock()
110{
111 std::cout << "high_resolution_clock test" << std::endl;
112 high_resolution_clock::duration delay = milliseconds(5);
113 high_resolution_clock::time_point start = high_resolution_clock::now();
114 while (high_resolution_clock::now() - start <= delay)
115 ;
116 high_resolution_clock::time_point stop = high_resolution_clock::now();
117 high_resolution_clock::duration elapsed = stop - start;
118 std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
119 start = high_resolution_clock::now();
120 stop = high_resolution_clock::now();
121 std::cout << "high_resolution_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
122}
123
124//void test_mixed_clock()
125//{
126// std::cout << "mixed clock test" << std::endl;
127// high_resolution_clock::time_point hstart = high_resolution_clock::now();
128// std::cout << "Add 5 milliseconds to a high_resolution_clock::time_point\n";
129// steady_clock::time_point mend = hstart + milliseconds(5);
130// bool b = hstart == mend;
131// system_clock::time_point sstart = system_clock::now();
132// std::cout << "Subtracting system_clock::time_point from steady_clock::time_point doesn't compile\n";
133//// mend - sstart; // doesn't compile
134// std::cout << "subtract high_resolution_clock::time_point from steady_clock::time_point"
135// " and add that to a system_clock::time_point\n";
136// system_clock::time_point send = sstart + duration_cast<system_clock::duration>(mend - hstart);
137// std::cout << "subtract two system_clock::time_point's and output that in microseconds:\n";
138// microseconds ms = send - sstart;
139// std::cout << ms.count() << " microseconds\n";
140//}
141//
142//void test_c_mapping()
143//{
144// std::cout << "C map test\n";
145// using namespace boost::chrono;
146// system_clock::time_point t1 = system_clock::now();
147// std::time_t c_time = system_clock::to_time_t(t1);
148// std::tm* tmptr = std::localtime(&c_time);
149// std::cout << "It is now " << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec << ' '
150// << tmptr->tm_year + 1900 << '-' << tmptr->tm_mon + 1 << '-' << tmptr->tm_mday << '\n';
151// c_time = std::mktime(tmptr);
152// system_clock::time_point t2 = system_clock::from_time_t(c_time);
153// microseconds ms = t1 - t2;
154// std::cout << "Round-tripping through the C interface truncated the precision by " << ms.count() << " microseconds\n";
155//}
156
157
158int main()
159{
160 test_system_clock();
161 test_steady_clock();
162 test_hi_resolution_clock();
163 //test_mixed_clock();
164 test_clock<system_clock>();
165#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
166 test_clock<steady_clock>();
167#endif
168 test_clock<high_resolution_clock>();
169
170
171
172 return 0;
173}
174
175#else
176int main()
177{
178
179
180 return 0;
181}
182#endif
183

source code of boost/libs/chrono/example/test_clock.cpp