High resolution timer with C++ and Linux?


Question

Under Windows there are some handy functions like QueryPerformanceCounter from mmsystem.h to create a high resolution timer. Is there something similar for Linux?

1
46
2/11/2009 8:24:54 PM

Accepted Answer

It's been asked before here -- but basically, there is a boost ptime function you can use, or a POSIX clock_gettime() function which can serve basically the same purpose.

32
5/23/2017 12:10:48 PM

For Linux (and BSD) you want to use clock_gettime().

#include <sys/time.h>

int main()
{
   timespec ts;
   // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
   clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux
}

See: This answer for more information


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon