0

So I need to do some precision timing and am endeavoring to use tv_sec and tv_usec. Right now I, super messily, have a vector of my own home-rolled Tuple class, holding both secs and microsecs.

The issue here is... if i want to say... get the oldest of the entries in my vector, I have to do super hairy and gross comparisons between the values in the tuples.

I'd really like to combine tv_sec and tv_usec into a single value to store. This would make comparison very easy.

But how does one do this safely? Is this attempt below going to blow up in my face for certain values, for example?

gettimeofday(&tv, NULL);
time_t secs = tv.tv_sec;
time_t msecs = tv.tv_usec;
double the_answer = tv_sec + (1.0/1000000) * tv_usec; //ehhhh? Maybe?

I spose the question boils down to - will overflow/act weird/ruin me if I use it:

    double the_answer = tv_sec + (1.0/1000000) * tv_usec; //ehhhh? Maybe?
PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
  • 1
    you can write a simple compare function? – billz Sep 28 '14 at 02:12
  • I could write a compare function, yes. In fact, I already have that function. I just figured it would be that tiny bit cleaner and nicer to combine it into a single parameter. Seems like a thing that may be useful at some later time, I reckon. – PinkElephantsOnParade Sep 28 '14 at 02:19
  • I would suggest no. Unnecessary convention introduces complexity and potential to introduce bugs. – billz Sep 28 '14 at 02:27
  • While I understand totally what you mean from a practical standpoint, I think it would be very instructive to see if this works. Not to mention it would unbloat my own personal code base. It's really a question of precision, I think. I wish I was more knowledgable on precision or it would be a rather simple yes or no. – PinkElephantsOnParade Sep 28 '14 at 02:34
  • double estimate = tv_sec + tv_usec/1000000000; – Grady Player Sep 28 '14 at 02:36
  • @GradyPlayer Cool, cool, that's essentially what I have! But it worries me you call it "estimate". Is it imprecise? I am intending to detect difference down to even 1 millisecond between values. Is there a chance this conversion squashes that? – PinkElephantsOnParade Sep 28 '14 at 02:37
  • Overload relational operators on `struct timeval`, that's your one entity. Ditch your tuple and be done with it. – n. m. could be an AI Sep 28 '14 at 02:54
  • @GradyPlayer: Do you call it "estimate" because the sub-second portion is wrong by a factor of 1000? – Nemo Sep 28 '14 at 03:09
  • @Nemo, lol... I apparently can't count... I swear most of the time I know u represents mu, which is 10e-6... which ... is where my counting breaks down... – Grady Player Sep 28 '14 at 13:04
  • double estimate = tv_sec + tv_usec/1000000; – Grady Player Sep 28 '14 at 13:05

1 Answers1

0

Just use std::chrono, or boost::chrono if you're stuck in the last millenium. The standard gives requirements for exactly how many bits are required for each unit, and the API is really nice.

The number of required bits (including the sign bit) is:

  • 23 for hours
  • 29 for minutes
  • 35 for seconds
  • 45 for milliseconds
  • 55 for microseconds
  • 64 for nanoseconds

For nanoseconds, these will work until the year 2262.

o11c
  • 15,265
  • 4
  • 50
  • 75