Alex Rivera | Logout

High resolution timer with C++ and Linux?

Asked 2009-02-11T20:24:54.097
47

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

Edit
Report

1 Answer

30

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

answered 2009-02-11T20:30:45.613

Your Answer