cheatah
Source

stdlib/time/time.cpp

1// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).
2// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.
3#include "time.hpp"
5#include <chrono>
6#include <ctime>
7#include <thread>
9namespace cheatah::time {
11namespace {
12using std::chrono::duration;
13using std::chrono::duration_cast;
14using std::chrono::nanoseconds;
15using std::chrono::steady_clock;
16using std::chrono::system_clock;
17} // namespace
19double time() { return duration<double>(system_clock::now().time_since_epoch()).count(); }
20long long time_ns() {
21 return duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
24double monotonic() { return duration<double>(steady_clock::now().time_since_epoch()).count(); }
25long long monotonic_ns() {
26 return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
29// steady_clock is the highest-resolution monotonic clock the platform offers.
30double perf_counter() { return duration<double>(steady_clock::now().time_since_epoch()).count(); }
31long long perf_counter_ns() {
32 return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
35double process_time() { return static_cast<double>(std::clock()) / CLOCKS_PER_SEC; }
37void sleep(double seconds) { std::this_thread::sleep_for(duration<double>(seconds)); }
39} // namespace cheatah::time