|
1 | 1 | /* |
2 | 2 | # chrono |
| 3 | +
|
| 4 | +What the clocks map to in GCC Linux: |
| 5 | +http://stackoverflow.com/questions/12392278/measure-time-in-linux-time-vs-clock-vs-getrusage-vs-clock-gettime-vs-gettimeof |
3 | 6 | */ |
4 | 7 |
|
5 | 8 | #include "common.hpp" |
6 | 9 |
|
7 | 10 | int main() { |
8 | 11 | #if __cplusplus >= 201103L |
9 | | - // TODO |
| 12 | + // # period |
10 | 13 | std::cout << "high_resolution_clock::period::den = " << std::chrono::high_resolution_clock::period::den << std::endl; |
| 14 | + std::cout << "steady_clock::period::den = " << std::chrono::steady_clock::period::den << std::endl; |
| 15 | + std::cout << "system_clock::period::den = " << std::chrono::system_clock::period::den << std::endl; |
11 | 16 |
|
| 17 | + // high_resolution_clock |
12 | 18 | // Wall clock: sleeps are counted. |
13 | | - std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now(); |
14 | | - std::this_thread::sleep_for(std::chrono::seconds(1)); |
15 | | - std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now(); |
16 | | - auto dt = end_time - start_time; |
17 | | - |
18 | | - std::cout << typeid(dt).name() << std::endl; |
19 | | - std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl; |
20 | | - std::cout << std::chrono::duration_cast<std::chrono::seconds>(dt).count() << std::endl; |
| 19 | + { |
| 20 | + std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now(); |
| 21 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 22 | + std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now(); |
| 23 | + std::chrono::duration<double, std::nano> dt = end_time - start_time; |
| 24 | + std::cout << "high_resolution_clock after sleep = " << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl; |
| 25 | + } |
| 26 | + |
| 27 | + // steady_clock |
| 28 | + { |
| 29 | + auto start_time = std::chrono::steady_clock::now(); |
| 30 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 31 | + auto end_time = std::chrono::steady_clock::now(); |
| 32 | + auto dt = end_time - start_time; |
| 33 | + std::cout << "steady_clock after sleep = " << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl; |
| 34 | + } |
| 35 | + |
| 36 | + // system_clock |
| 37 | + { |
| 38 | + auto start_time = std::chrono::system_clock::now(); |
| 39 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 40 | + auto end_time = std::chrono::system_clock::now(); |
| 41 | + auto dt = end_time - start_time; |
| 42 | + std::cout << "system_clock after sleep = " << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl; |
| 43 | + } |
| 44 | + |
| 45 | + /* |
| 46 | + # time_point |
| 47 | +
|
| 48 | + Convertions to base types: |
| 49 | +
|
| 50 | + - http://stackoverflow.com/questions/12835577/how-to-convert-stdchronotime-point-to-calendar-datetime-string-with-fraction |
| 51 | + - http://stackoverflow.com/questions/33785564/how-do-i-get-seconds-since-epoch-as-a-double-given-a-time-point |
| 52 | + - http://stackoverflow.com/questions/31255486/c-how-do-i-convert-a-stdchronotime-point-to-long-and-back |
| 53 | + */ |
| 54 | + { |
| 55 | + std::cout << "nanoseconds since system_clock epoch = " << std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() << std::endl; |
| 56 | + std::cout << "nanoseconds since steady_clock epoch = " << std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() << std::endl; |
| 57 | + } |
21 | 58 | return 0; |
22 | 59 | #endif |
23 | 60 | } |
0 commit comments