Browse Source

Slightly increase performance of system::getNanoseconds() for Linux.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
58a44db0c5
2 changed files with 11 additions and 3 deletions
  1. +3
    -2
      include/system.hpp
  2. +8
    -1
      src/system.cpp

+ 3
- 2
include/system.hpp View File

@@ -36,8 +36,9 @@ int getLogicalCoreCount();
void setThreadName(const std::string& name);
/** Returns the caller's human-readable stack trace with "\n"-separated lines. */
std::string getStackTrace();
/** Returns the current number of nanoseconds since the epoch with the highest precion available on the OS.
The epoch is undefined and could be the UNIX epoch, time since boot, time since launch, etc.
/** Returns the current number of nanoseconds since the epoch.
The goal of this function is to give the most precise (fine-grained) time available on the OS for benchmarking purposes, while being fast to compute.
The epoch is undefined. Do not use this function to get absolute time, as it is different on each OS.
*/
int64_t getNanoseconds();
/** Opens a URL, also happens to work with PDFs and folders.


+ 8
- 1
src/system.cpp View File

@@ -225,7 +225,14 @@ int64_t getNanoseconds() {
int64_t nsPerTick = 1000000000LL / frequency.QuadPart;
int64_t time = counter.QuadPart * nsPerTick;
return time;
#else
#endif
#if defined ARCH_LIN
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
int64_t time = int64_t(ts.tv_sec) * 1000000000LL + ts.tv_nsec;
return time;
#endif
#if defined ARCH_MAC
using clock = std::chrono::high_resolution_clock;
using time_point = std::chrono::time_point<clock>;
time_point now = clock::now();


Loading…
Cancel
Save