Utku Zihnioglu
Utku Zihnioglu

Reputation: 4883

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

I am currently using clock() function supplied by the time.h library. It gives me time precision up to milliseconds. However, it's timing is based on CPU clock cycles. I need a function that instead of using CPU cycles as clock(), will use system realtime with precision up to milliseconds.

I am using linux with gcc compiler.

Upvotes: 5

Views: 6842

Answers (4)

chrisaycock
chrisaycock

Reputation: 37928

#include <sys/time.h>

...

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

Upvotes: 8

Chinmoy
Chinmoy

Reputation: 1754

CLOCK_REALTIME

gives nanoseconds. Go no further, You might start a space-warp. :)

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Check out clock_gettime(2).

Upvotes: 2

aschepler
aschepler

Reputation: 72473

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

Upvotes: 3

Related Questions