Home > Backend Development > C++ > When Should You (and Shouldn't You) Use `std::chrono::high_resolution_clock`?

When Should You (and Shouldn't You) Use `std::chrono::high_resolution_clock`?

Barbara Streisand
Release: 2024-12-31 10:04:17
Original
843 people have browsed it

When Should You (and Shouldn't You) Use `std::chrono::high_resolution_clock`?

Delving into the Usefulness of std::chrono::high_resolution_clock

Although initially considered suitable for performance measurements, the use of std::chrono::high_resolution_clock raises concerns due to potential clock adjustments and an inability to convert to calendar time. This leaves the question of its practical applications.

Scrutinizing Possible Uses

  • Performance Measurements: The unsteady nature of high_resolution_clock makes time interval measurements unreliable.
  • Retrieving Real Time: The absence of a to_time_t method precludes the acquisition of calendar time.

Conclusion: An Elusive Purpose

After examining the limitations of high_resolution_clock, it becomes evident that it lacks substantial utility. Its use should be avoided in favor of the more reliable steady_clock.

Exceptions to Avoidance

While generally discouraged, there are specific situations where high_resolution_clock may be appropriate:

  • On platforms where high_resolution_clock is implemented as a steady_clock alias, such as libc or VS.
  • Situations where truncation towards zero is desired, achieved by explicitly using duration_cast.
  • Instances where implicit conversions cannot be applied.

However, these exceptions should be considered with caution and alternative approaches explored whenever possible.

The above is the detailed content of When Should You (and Shouldn't You) Use `std::chrono::high_resolution_clock`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template