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 /*
11 This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
12 was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
13 Many thanks to Howard for making his code available under the Boost license.
14 The original code was modified to conform to Boost conventions and to section
15 20.9 Time utilities [time] of the C++ committee's working paper N2798.
16 See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
17 
18 time2_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 namespace boost {
37     namespace detail_chrono {
38         class steady_clock {};
39         class system_clock {};
40     }
41     namespace chrono {
42         namespace chrono_detail {
43             using namespace detail_chrono;
44             struct has_steady_clock {
45                 template< class T > static char sfinae( typename T::rep );
46                 template< class >   static int sfinae( ... );
47 
48                 enum { value = sizeof sfinae< steady_clock >( 0 ) == sizeof(char) };
49             };
50             struct has_system_clock {
51                 template< class T > static char sfinae( typename T::rep );
52                 template< class >   static int sfinae( ... );
53 
54                 enum { value = sizeof sfinae< system_clock >( 0 ) == sizeof(char) };
55             };
56         }
57         struct has_steady_clock
58             : integral_constant<bool, chrono_detail::has_steady_clock::value> {};
59         struct has_system_clock
60             : integral_constant<bool, chrono_detail::has_system_clock::value> {};
61     }
62 
63 }
64 
65 BOOST_STATIC_ASSERT(boost::chrono::has_system_clock::value);
66 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
67 BOOST_STATIC_ASSERT(boost::chrono::has_steady_clock::value);
68 #else
69 BOOST_STATIC_ASSERT(!boost::chrono::has_steady_clock::value);
70 #endif
71 
72 using namespace boost::chrono;
73 using namespace boost;
74 
75 template <typename Clock>
test_clock()76 void test_clock()
77 {
78     std::cout << "\n"<< name<Clock>::apply() << " test" << std::endl;
79 {
80     typename Clock::duration delay = milliseconds(5);
81     typename Clock::time_point start = Clock::now();
82     while (Clock::now() - start <= delay)
83         ;
84     typename Clock::time_point stop = Clock::now();
85     //typename Clock::duration elapsed = stop - start;
86     std::cout << "5 milliseconds paused " << nanoseconds(stop - start).count() << " nanoseconds\n";
87 }
88 {
89     typename Clock::time_point start = Clock::now();
90     typename Clock::time_point stop;
91     std::size_t count=1;
92     while ((stop=Clock::now()) == start) {
93         ++count;
94     }
95     //typename Clock::duration elapsed = stop - start;
96     std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
97 
98     start = Clock::now();
99     for (std::size_t c=count; c>0; --c) {
100         stop=Clock::now();;
101     }
102     std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n";
103 
104 
105 }
106 {
107     typename Clock::time_point start = Clock::now();
108     typename Clock::time_point stop = Clock::now();
109     std::cout << "Resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
110 }
111 }
112 
test_system_clock()113 void test_system_clock()
114 {
115     std::cout << "system_clock test" << std::endl;
116     //~ system_clock  clk;
117     chrono::system_clock::duration delay = milliseconds(5);
118     chrono::system_clock::time_point start = system_clock::now();
119     while (chrono::system_clock::now() - start <= delay)
120         ;
121     chrono::system_clock::time_point stop = system_clock::now();
122     chrono::system_clock::duration elapsed = stop - start;
123     std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
124     start = chrono::system_clock::now();
125     stop = chrono::system_clock::now();
126     std::cout << "system_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
127 }
128 
test_steady_clock()129 void test_steady_clock()
130 {
131 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
132     std::cout << "steady_clock test" << std::endl;
133     steady_clock::duration delay = milliseconds(5);
134     steady_clock::time_point start = steady_clock::now();
135     while (steady_clock::now() - start <= delay)
136         ;
137     steady_clock::time_point stop = steady_clock::now();
138     steady_clock::duration elapsed = stop - start;
139     std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
140     start = steady_clock::now();
141     stop = steady_clock::now();
142     std::cout << "steady_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
143 #endif
144 }
test_hi_resolution_clock()145 void test_hi_resolution_clock()
146 {
147     std::cout << "high_resolution_clock test" << std::endl;
148     high_resolution_clock::duration delay = milliseconds(5);
149     high_resolution_clock::time_point start = high_resolution_clock::now();
150     while (high_resolution_clock::now() - start <= delay)
151       ;
152     high_resolution_clock::time_point stop = high_resolution_clock::now();
153     high_resolution_clock::duration elapsed = stop - start;
154     std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n";
155     start = high_resolution_clock::now();
156     stop = high_resolution_clock::now();
157     std::cout << "high_resolution_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n";
158 }
159 
160 //void test_mixed_clock()
161 //{
162 //    std::cout << "mixed clock test" << std::endl;
163 //    high_resolution_clock::time_point hstart = high_resolution_clock::now();
164 //    std::cout << "Add 5 milliseconds to a high_resolution_clock::time_point\n";
165 //    steady_clock::time_point mend = hstart + milliseconds(5);
166 //    bool b = hstart == mend;
167 //    system_clock::time_point sstart = system_clock::now();
168 //    std::cout << "Subtracting system_clock::time_point from steady_clock::time_point doesn't compile\n";
169 ////  mend - sstart; // doesn't compile
170 //    std::cout << "subtract high_resolution_clock::time_point from steady_clock::time_point"
171 //            " and add that to a system_clock::time_point\n";
172 //    system_clock::time_point send = sstart + duration_cast<system_clock::duration>(mend - hstart);
173 //    std::cout << "subtract two system_clock::time_point's and output that in microseconds:\n";
174 //    microseconds ms = send - sstart;
175 //    std::cout << ms.count() << " microseconds\n";
176 //}
177 //
178 //void test_c_mapping()
179 //{
180 //    std::cout << "C map test\n";
181 //    using namespace boost::chrono;
182 //    system_clock::time_point t1 = system_clock::now();
183 //    std::time_t c_time = system_clock::to_time_t(t1);
184 //    std::tm* tmptr = std::localtime(&c_time);
185 //    std::cout << "It is now " << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec << ' '
186 //              << tmptr->tm_year + 1900 << '-' << tmptr->tm_mon + 1 << '-' << tmptr->tm_mday << '\n';
187 //    c_time = std::mktime(tmptr);
188 //    system_clock::time_point t2 = system_clock::from_time_t(c_time);
189 //    microseconds ms = t1 - t2;
190 //    std::cout << "Round-tripping through the C interface truncated the precision by " << ms.count() << " microseconds\n";
191 //}
192 
193 
main()194 int main()
195 {
196     test_system_clock();
197     test_steady_clock();
198     test_hi_resolution_clock();
199     //test_mixed_clock();
200     test_clock<system_clock>();
201 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
202     test_clock<steady_clock>();
203 #endif
204     test_clock<high_resolution_clock>();
205 
206 
207 
208     return 0;
209 }
210 
211