logoalt Hacker News

mpynetoday at 2:08 AM1 replyview on HN

std::chrono counts the number of ticks of a given periodicity. The periodicity does have to be a known compile-time ratio expressible as a fraction of seconds, but you can use a floating-type type to count the number of ticks.

std::chrono is meant to take advantage of information about periodicity at compile time, but if you want to count in terms of a dynamic periodicity not known until runtime, you can still do things. E.g. use time_t and some wrapper functions, or just pick one (or several) std::chrono base durations such as standardizing on nanoseconds, and then just count floating-point ticks.


Replies

loegtoday at 4:21 AM

> The periodicity does have to be a known compile-time ratio expressible as a fraction of seconds

Right, that's the problem I'm describing.

> or just pick one (or several) std::chrono base durations such as standardizing on nanoseconds, and then just count floating-point ticks.

None of this really fits well -- the idea is to count in integer ticks (invariant cycles), without doing expensive division or floating point math. It's like steady clock, but in ticks instead of nanos.

show 1 reply