所以我需要做一些精确计时,并努力使用tv_sec和tv_usec。 现在我,超级凌乱,有一个我自己的家庭卷Tuple类的向量,持有秒和微秒。
这里的问题是......如果我想说...获取我的向量中最老的条目,我必须对元组中的值进行超毛和粗略的比较。
我真的想将tv_sec和tv_usec组合成一个值来存储。 这将使比较变得非常容易。
但是如何安全地做到这一点? 例如,这种尝试是否会在某些价值观中爆炸?
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?我把问题归结为 - 如果我使用它会溢出/行为怪异/毁了我:
double the_answer = tv_sec + (1.0/1000000) * tv_usec; //ehhhh? Maybe?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?最满意答案
如果你被困在最后一个千禧年,只需使用std::chrono或boost::chrono chrono。 该标准给出了每个单元所需的精确位数的要求,并且API 非常好。
所需位数(包括符号位)为:
23小时 29分钟 35秒 45毫秒 55微秒 64纳秒对于纳秒,这些将持续到2262年。
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 nanosecondsFor nanoseconds, these will work until the year 2262.
更多推荐
发布评论