Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...