|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright Christopher Kormanyos 2013 - 2024. |
| 3 | +// Distributed under the Boost Software License, |
| 4 | +// Version 1.0. (See accompanying file LICENSE_1_0.txt |
| 5 | +// or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | + |
| 8 | +#ifndef STOPWATCH_2024_03_28_H // NOLINT(llvm-header-guard) |
| 9 | + #define STOPWATCH_2024_03_28_H |
| 10 | + |
| 11 | + #include <cstdint> |
| 12 | + #include <ctime> |
| 13 | + |
| 14 | + #if defined(_MSC_VER) && !defined(__GNUC__) |
| 15 | + #define STOPWATCH_NODISCARD |
| 16 | + #else |
| 17 | + #if (defined(__cplusplus) && (__cplusplus >= 201703L)) |
| 18 | + #define STOPWATCH_NODISCARD [[nodiscard]] // NOLINT(cppcoreguidelines-macro-usage) |
| 19 | + #else |
| 20 | + #define STOPWATCH_NODISCARD |
| 21 | + #endif |
| 22 | + #endif |
| 23 | + |
| 24 | + // See also: https://godbolt.org/z/37a4n9f4Y |
| 25 | + |
| 26 | + namespace concurrency { |
| 27 | + |
| 28 | + struct stopwatch |
| 29 | + { |
| 30 | + public: |
| 31 | + using time_point_type = std::uintmax_t; |
| 32 | + |
| 33 | + stopwatch() |
| 34 | + { |
| 35 | + reset(); |
| 36 | + } |
| 37 | + |
| 38 | + auto reset() -> void |
| 39 | + { |
| 40 | + timespec ts { }; |
| 41 | + |
| 42 | + timespec_get(&ts, TIME_UTC); |
| 43 | + |
| 44 | + m_start = |
| 45 | + static_cast<time_point_type> |
| 46 | + ( |
| 47 | + static_cast<time_point_type>(static_cast<time_point_type>(ts.tv_sec) * UINTMAX_C(1000000000)) |
| 48 | + + static_cast<time_point_type>(ts.tv_nsec) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + template<typename RepresentationRequestedTimeType> |
| 53 | + static auto elapsed_time(const stopwatch& my_stopwatch) noexcept -> RepresentationRequestedTimeType |
| 54 | + { |
| 55 | + using local_time_type = RepresentationRequestedTimeType; |
| 56 | + |
| 57 | + return |
| 58 | + local_time_type |
| 59 | + { |
| 60 | + static_cast<local_time_type>(my_stopwatch.elapsed()) |
| 61 | + / local_time_type { UINTMAX_C(1000000000) } |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + private: |
| 66 | + time_point_type m_start { }; // NOLINT(readability-identifier-naming) |
| 67 | + |
| 68 | + STOPWATCH_NODISCARD auto elapsed() const -> time_point_type |
| 69 | + { |
| 70 | + timespec ts { }; |
| 71 | + |
| 72 | + timespec_get(&ts, TIME_UTC); |
| 73 | + |
| 74 | + time_point_type |
| 75 | + stop |
| 76 | + { |
| 77 | + static_cast<time_point_type> |
| 78 | + ( |
| 79 | + static_cast<time_point_type>(static_cast<time_point_type>(ts.tv_sec) * UINTMAX_C(1000000000)) |
| 80 | + + static_cast<time_point_type>(ts.tv_nsec) |
| 81 | + ) |
| 82 | + }; |
| 83 | + |
| 84 | + const std::uintmax_t |
| 85 | + elapsed_ns |
| 86 | + { |
| 87 | + static_cast<time_point_type> |
| 88 | + ( |
| 89 | + stop - m_start |
| 90 | + ) |
| 91 | + }; |
| 92 | + |
| 93 | + return elapsed_ns; |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + } // namespace concurrency |
| 98 | + |
| 99 | +#endif // STOPWATCH_2024_03_28_H |
0 commit comments