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>9
namespace cheatah::time {11
namespace {12
using std::chrono::duration;13
using std::chrono::duration_cast;14
using std::chrono::nanoseconds;15
using std::chrono::steady_clock;16
using std::chrono::system_clock;17
} // namespace19
double time() { return duration<double>(system_clock::now().time_since_epoch()).count(); }20
long long time_ns() {21
return duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();22
}24
double monotonic() { return duration<double>(steady_clock::now().time_since_epoch()).count(); }25
long long monotonic_ns() {26
return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();27
}29
// steady_clock is the highest-resolution monotonic clock the platform offers.30
double perf_counter() { return duration<double>(steady_clock::now().time_since_epoch()).count(); }31
long long perf_counter_ns() {32
return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();33
}35
double process_time() { return static_cast<double>(std::clock()) / CLOCKS_PER_SEC; }37
void sleep(double seconds) { std::this_thread::sleep_for(duration<double>(seconds)); }39
} // namespace cheatah::time