Qt has QElapsedTimer
which supports measuring up to nanoseconds. I cannot testify to how accurate it is, IIRC it uses different implementations on different platforms. Sadly it is C++, which may not suit you. Also:
On platforms that do not provide nanosecond resolution, the value
returned will be the best estimate available.
The clock()
function is alright for rough measurements, but it works in the millisecond range. Contrary to its name, I don't think it measures in CPU clocks, since modern processors clock frequencies can vary quite a bit, making it impossible to determine the actual time accurately solely relying on CPU clocks. IMO this concept dates back to the days when CPU frequencies were constant, there was no power management, no "turbo boost" automatic overclocking or whatsoever.
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.