A clock consists of a starting point and a time tick. C++ offers with std::chrono::system_clock, std::chrono::steady_clock, and std::chrono::high_resolution_clock three clocks.
Because of three different clocks, there is the question: What are the differences?
- std::chrono::sytem_clock: This is the system-wide real-time clock (wall-clock). The clock has the auxiliary functions to_time_t and from_time_t to convert time points into dates.
- std::chrono::steady_clock: Provides as only a clock the guarantee that you can not adjust it. Therefore, std::chrono::steady_clock is the preferred clock to wait for a time duration or until a time point.
- std::chrono::high_resolution_clock: This is the clock with the highest accuracy, but it can be a synonym for the clock’s std::chrono::system_clock or std::chrono::steady_clock.
The C++ standard provides no guarantee about the clocks’ accuracy, starting point, or valid time range. Typically, the starting point of std::chrono:system_clock is the 1.1.1970, the so-called UNIX-epoch. For std::chrono::steady_clock, typically the boot time of your PC.
Accuracy and Steadiness
It’s interesting to know which clocks are steady and which accuracy they provide. You get the answers from the clocks.
I display in lines 22, 28, and 34 whether each clock is continuous. My job in the function printRatio (lines 7 – 15) is more challenging. First, I show the accuracy of the clocks in a fraction, and second, in a floating number. Therefore, I use the function template std::ratio_multiply and the constants std::kilo and std::mega to adjust the units to milliseconds and microseconds. You can get the details on the calculation at compile time at cppreference.com.
The output on Linux differs from that on Windows. std::chrono::system_clock is far more accurate on Linux; std::chrono::high_resultion_clock is steady on Windows.
Although the C++ standard doesn’t specify the epoch of the clock, you can calculate it.
Thanks to the auxiliary function time_since_epoch, you get how much time has passed since the epoch from each time point.
The variables timeDurSysClock (line 24), timeNowStClock (line 31), and timeNowHiResClock (Zeile 38) hold for each clock, how much time has passed since the starting point of the clock. When I use no automatic type deduction with auto, explicit types of the time point and time duration are extremely verbose to write. In the function durationSinceEpoch (lines 7 – 17), I show the time duration in different resolutions. First, I display the number of time ticks (line 9), then the number of minutes (line 13), and at the end of the years (line 16) since the epoch, all depending on the used clock. I ignore leap years for simplicity, and my year has 365 days.
The results are different on Linux and Windows.
To draw the right conclusion, I must mention that my Linux PC runs for about 5 hours (305 minutes) and my Windows PC for more than 6 hours (391 minutes).
std::chrono::system_clock and std::chrono::high_resolution_clock have on Linux the UNIX-epoch as starting point. The starting point of std::chrono::steady_clock is the boot time of my PC. The difference between Linux and Windows is std::high_resolution_clock. On Linux, the std::chrono::system_clock is internally used; on Windows, the std::chrono::steady_clock is internally used.
That is not the end of the story about the new time library. With the component’s time point and time duration, you can put a thread for an absolute or relative time to sleep. The details will follow in the next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,