Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the unit of gettimeofday()?

I've a program to calculate the latency of an object in a pub-sub model. I've used the following function for timestamp:

uint64_t GetTimeStamp() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
}

The latency is measured as timestamp difference in publisher and subscriber. So, I'm concerned about the unit of the latency measured. Is it in seconds or microseconds??

like image 932
John Avatar asked Jul 25 '13 06:07

John


People also ask

What does gettimeofday do in c?

The gettimeofday() function retrieves the current Coordinated Universal Time (UTC) and places it in the timeval structure pointed to by tp . If tzp is not NULL, the time zone information is returned in the time zone structure pointed to by tzp .

What is Tv_sec?

long int tv_sec. This represents the number of whole seconds of elapsed time. long int tv_usec. This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds.


1 Answers

The timeval structure has tv_sec, which gives you the absolute value for the seconds, and tv_usec, which gives you the remaining fraction in micro seconds.

So, you could get the resolution in micro seconds.

For more information, http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html

like image 164
Karthikeyan Avatar answered Oct 02 '22 00:10

Karthikeyan