| 1 | /////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // A timer class designed for dealing with timestamps |
| 4 | // CK Nov 2002 |
| 5 | // |
| 6 | /////////////////////////////////////////////////////// |
| 7 | |
| 8 | #include "cvd/timer.h" |
| 9 | #include <chrono> |
| 10 | #include <iostream> |
| 11 | |
| 12 | using namespace std; |
| 13 | using namespace std::chrono; |
| 14 | |
| 15 | namespace CVD |
| 16 | { |
| 17 | |
| 18 | long long get_time_of_day_ns() |
| 19 | { |
| 20 | auto time = high_resolution_clock::now(); |
| 21 | |
| 22 | return time_point_cast<chrono::nanoseconds>(time).time_since_epoch().count(); |
| 23 | } |
| 24 | |
| 25 | cvd_timer::cvd_timer() |
| 26 | { |
| 27 | start = high_resolution_clock::now(); |
| 28 | } |
| 29 | |
| 30 | double cvd_timer::reset() |
| 31 | { |
| 32 | auto now = high_resolution_clock::now(); |
| 33 | double r = duration<float>(now - start).count(); |
| 34 | start = now; |
| 35 | |
| 36 | return r; |
| 37 | } |
| 38 | |
| 39 | double cvd_timer::get_time() |
| 40 | { |
| 41 | auto now = high_resolution_clock::now(); |
| 42 | return duration<float>(now - start).count(); |
| 43 | } |
| 44 | |
| 45 | double get_time_of_day() |
| 46 | { |
| 47 | return static_cast<double>(get_time_of_day_ns()) / 1e9; |
| 48 | } |
| 49 | |
| 50 | double cvd_timer::conv_ntime(const double& time) const |
| 51 | { |
| 52 | double start_seconds = time_point_cast<duration<float>>(start).time_since_epoch().count(); |
| 53 | return time - start_seconds; |
| 54 | } |
| 55 | |
| 56 | cvd_timer timer; |
| 57 | |
| 58 | } |