What is the unit of gettimeofday()?
Asked Answered
V

2

5

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??

Vocation answered 25/7, 2013 at 6:26 Comment(5)
man gettimeofday to see struct timeval detailsMong
Just simply look with what constant you are multiplying seconds to get something else. 1sec = 1000000 microseconds.Endosteum
pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/… :)Bradley
Strictly speaking, that's the unit of reporting, not measurement.Cypro
If you have C++11 (gcc 4.7.2+), look at std::chrono (cppreference has a good example), for an example usage with milliseconds: daniweb.com/software-development/cpp/code/445750/…. One of the nice things about std::chrono is a quick search and replace on "milliseconds" -> "microseconds" or "nanoseconds" and you have that resolution.Extrude
P
8

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

Puttyroot answered 25/7, 2013 at 6:31 Comment(0)
S
4

tv.tv_sec gives the second count, tv.tv_usec gives the remaining microsecond count.

For gettimeofday() principle and its accuracy:

How is the microsecond time of linux gettimeofday() obtained and what is its accuracy?

Is gettimeofday() guaranteed to be of microsecond resolution?

Schilling answered 25/7, 2013 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.