Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the uses of std::chrono::high_resolution_clock?

At first I thought it can be used for performance measurements. But it is said that std::chrono::high_resolution_clock may be not steady (is_steady may be false). It is also said that std::chrono::high_resolution_clock may even be an alias of std::chrono::system_clock which is generally not steady. So I can't measure time intervals with this type of clock because at any moment the clock may be adjusted and my measurements will be wrong.

At the same time I can't convert time points of std::chrono::high_resolution_clock to calendar time because it doesn't have to_time_t method. So I can't get the real time with this type of clock either.

Then what can std::chrono::high_resolution_clock be used for?

like image 432
anton_rh Avatar asked May 25 '16 02:05

anton_rh


People also ask

What does std :: Chrono :: High_resolution_clock :: now return?

std::chrono::high_resolution_clock::now Returns the current time_point in the frame of the high_resolution_clock.

What is Chrono High_resolution_clock?

Defined in header <chrono> class high_resolution_clock; (since C++11) Class std::chrono::high_resolution_clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.

What is STD Chrono in C++?

(since C++11) Class template std::chrono::duration represents a time interval. It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational fraction representing the time in seconds from one tick to the next.

What does std :: Chrono :: System_clock :: now return?

std::chrono::system_clock::now Returns a time point representing with the current point in time.


2 Answers

There are none.

Sorry, my bad.

If you are tempted to use high_resolution_clock, choose steady_clock instead. On libc++ and VS high_resolution_clock is a type alias of steady_clock anyway.

On gcc high_resolution_clock is a type alias of system_clock and I've seen more than one use of high_resolution_clock::to_time_t on this platform (which is wrong).

Do use <chrono>. But there are parts of <chrono> that you should avoid.

  • Don't use high_resolution_clock.
  • Avoid uses of .count() and .time_since_epoch() unless there is no other way to get the job done.
  • Avoid duration_cast unless the code won't compile without it, and you desire truncation-towards-zero behavior.
  • Avoid explicit conversion syntax if an implicit conversion compiles.
like image 53
Howard Hinnant Avatar answered Oct 16 '22 06:10

Howard Hinnant


I would suggest that single uses of the high res clock could lie.

If your algorithm makes use of many measurements then any error should average-out and you should be able to measure very small intervals.

I've used the high resolution clock to build timelines in network events where otherwise my intervals would have all been 0. :)

Finally, consider that while your system clock might not be steady, if your high res clock is stead, that greatly simplifies some games you have to play to make sure time always flows forwards.

In summary, yeah, it can fail, but when you have it, it's very helpful. Lots of samples are your friends. :)

like image 26
Sam Avatar answered Oct 16 '22 05:10

Sam