Qt has a QElapsedTimer that supports measurement up to nanoseconds. I canβt testify to how accurate this is; IIRC uses various implementations on different platforms. Unfortunately, this is C ++, which may not suit you. Also:
On platforms that do not provide nanosecond resolution, a Return value would be a better estimate.
The clock() function is suitable for rough measurements, but works in milliseconds. Contrary to its name, I do not think that it measures the processor clock, since the modern processor clock speeds can vary quite strongly, which makes it impossible to accurately determine the actual time solely on the basis of the CPU clock pulses. IMO, this concept dates back to the times when the processor frequencies were constant, there was no power management, automatic acceleration without turbocharging, and in general.
EDIT: Also found this (time.h):
int clock_gettime(clockid_t clk_id, struct timespec *tp); ... and the target struct... struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; ... and the clock options... CLOCK_REALTIME System-wide realtime clock. Setting this clock requires appropriate privileges. CLOCK_MONOTONIC Clock that cannot be set and represents monotonic time since some unspecified starting point. CLOCK_PROCESS_CPUTIME_ID High-resolution per-process timer from the CPU. CLOCK_THREAD_CPUTIME_ID Thread-specific CPU-time clock.
dtech
source share