Download cpp_engine/include/core/time_utils.hpp from Ypeng12/quant-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/spaces/Ypeng12/quant-ai/resolve/main/cpp_engine/include/core/time_utils.hpp
- Command line
-
hf download hf://spaces/Ypeng12/quant-ai/cpp_engine/include/core/time_utils.hpp
-
curl -L -o time_utils.hpp https://huggingface.co/spaces/Ypeng12/quant-ai/resolve/main/cpp_engine/include/core/time_utils.hpp
1.72 kB
| namespace quant::core { | |
| class TimeUtils { | |
| public: | |
| /** | |
| * Get monotonic time in nanoseconds since boot. | |
| * Guaranteed strictly monotonic and non-decreasing. | |
| */ | |
| static inline uint64_t now_ns() noexcept { | |
| struct timespec ts; | |
| clock_gettime(CLOCK_MONOTONIC_RAW, &ts); | |
| return static_cast<uint64_t>(ts.tv_sec) * 1000000000ULL + static_cast<uint64_t>(ts.tv_nsec); | |
| auto now = std::chrono::steady_clock::now().time_since_epoch(); | |
| return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count(); | |
| } | |
| /** | |
| * Get wall clock time in nanoseconds since Unix epoch. | |
| */ | |
| static inline uint64_t unix_epoch_ns() noexcept { | |
| auto now = std::chrono::system_clock::now().time_since_epoch(); | |
| return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count(); | |
| } | |
| /** | |
| * Read CPU timestamp counter (RDTSC on x86_64). | |
| */ | |
| static inline uint64_t rdtsc() noexcept { | |
| uint32_t hi, lo; | |
| __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); | |
| return (static_cast<uint64_t>(hi) << 32) | lo; | |
| return now_ns(); | |
| } | |
| }; | |
| /** | |
| * RAII Latency Measurement Utility | |
| */ | |
| template <typename Callback> | |
| class ScopedTimer { | |
| public: | |
| explicit ScopedTimer(Callback&& cb) : cb_(std::forward<Callback>(cb)), start_ns_(TimeUtils::now_ns()) {} | |
| ~ScopedTimer() { | |
| uint64_t elapsed_ns = TimeUtils::now_ns() - start_ns_; | |
| cb_(elapsed_ns); | |
| } | |
| private: | |
| Callback cb_; | |
| uint64_t start_ns_; | |
| }; | |
| } // namespace quant::core | |