| 1 | // await_keystroke.cpp -----------------------------------------------------// |
|---|---|
| 2 | |
| 3 | // Copyright Beman Dawes 2008 |
| 4 | |
| 5 | // Distributed under the Boost Software License, Version 1.0. |
| 6 | // See http://www.boost.org/LICENSE_1_0.txt |
| 7 | |
| 8 | #define _CRT_SECURE_NO_WARNINGS |
| 9 | |
| 10 | #include <boost/chrono/chrono.hpp> |
| 11 | #include <iostream> |
| 12 | #include <iomanip> |
| 13 | |
| 14 | using namespace boost::chrono; |
| 15 | |
| 16 | template< class Clock > |
| 17 | class timer |
| 18 | { |
| 19 | typename Clock::time_point start; |
| 20 | public: |
| 21 | |
| 22 | timer() : start( Clock::now() ) {} |
| 23 | |
| 24 | typename Clock::duration elapsed() const |
| 25 | { |
| 26 | return Clock::now() - start; |
| 27 | } |
| 28 | |
| 29 | double seconds() const |
| 30 | { |
| 31 | return elapsed().count() * ((double)Clock::period::num/Clock::period::den); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | int main() |
| 36 | { |
| 37 | timer<system_clock> t1; |
| 38 | timer<steady_clock> t2; |
| 39 | timer<high_resolution_clock> t3; |
| 40 | |
| 41 | std::cout << "Strike any key: "; |
| 42 | std::cin.get(); |
| 43 | |
| 44 | std::cout << std::fixed << std::setprecision(9); |
| 45 | std::cout << "system_clock-----------: " |
| 46 | << t1.seconds() << " seconds\n"; |
| 47 | std::cout << "steady_clock--------: " |
| 48 | << t2.seconds() << " seconds\n"; |
| 49 | std::cout << "high_resolution_clock--: " |
| 50 | << t3.seconds() << " seconds\n"; |
| 51 | |
| 52 | system_clock::time_point d4 = system_clock::now(); |
| 53 | system_clock::time_point d5 = system_clock::now(); |
| 54 | |
| 55 | std::cout << "\nsystem_clock latency-----------: "<< (d5 - d4).count() << std::endl; |
| 56 | |
| 57 | steady_clock::time_point d6 = steady_clock::now(); |
| 58 | steady_clock::time_point d7 = steady_clock::now(); |
| 59 | |
| 60 | std::cout << "steady_clock latency--------: "<< (d7 - d6).count() << std::endl; |
| 61 | |
| 62 | high_resolution_clock::time_point d8 = high_resolution_clock::now(); |
| 63 | high_resolution_clock::time_point d9 = high_resolution_clock::now(); |
| 64 | |
| 65 | std::cout << "high_resolution_clock latency--: "<< (d9 - d8).count() << std::endl; |
| 66 | |
| 67 | std::time_t now = system_clock::to_time_t(t: system_clock::now()); |
| 68 | |
| 69 | std::cout << "\nsystem_clock::now() reports UTC is " |
| 70 | << std::asctime(tp: std::gmtime(timer: &now)) << "\n"; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 |
