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

John
  • 73
  • 1
  • 1
  • 4
  • 2
    `man gettimeofday` to see struct timeval details – suspectus Jul 25 '13 at 06:29
  • Just simply look with what constant you are multiplying seconds to get something else. 1sec = 1000000 microseconds. – darxsys Jul 25 '13 at 06:29
  • http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/time.h.html :) – ludesign Jul 25 '13 at 06:29
  • Strictly speaking, that's the unit of _reporting_, not measurement. – MSalters Jul 25 '13 at 06:30
  • If you have C++11 (gcc 4.7.2+), look at [std::chrono](http://en.cppreference.com/w/cpp/chrono) (cppreference has a good example), for an example usage with milliseconds: http://www.daniweb.com/software-development/cpp/code/445750/simple-timer-using-c11s-chrono-library. One of the nice things about std::chrono is a quick search and replace on "milliseconds" -> "microseconds" or "nanoseconds" and you have that resolution. – kfsone Jul 25 '13 at 08:01

2 Answers2

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

jogojapan
  • 68,383
  • 11
  • 101
  • 131
Karthikeyan
  • 990
  • 5
  • 12
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?

Community
  • 1
  • 1
lulyon
  • 6,707
  • 7
  • 32
  • 49