What is a good way to find real time in milliseconds using C ++?

I am currently using the function clock()provided by the library time.h. This gives me time accuracy of up to milliseconds. However, this time is based on the processor cycle. I need a function that, instead of using processor cycles, it clock()will use the system real time accurate to the millisecond.

I am using linux with the gcc compiler.

+5
source share
4 answers
#include <sys/time.h>

...

timeval tv;
gettimeofday (&tv, NULL);
return double (tv.tv_sec) + 0.000001 * tv.tv_usec;
+8
source

Linux (and POSIX.1-2001) provides gettimeofday()to get the current time to microsecond accuracy.

+3

CLOCK_REALTIME

gives nanoseconds. Go ahead, you can start space deformation. :)

0
source

All Articles