The Three Clocks

Contents[Show]

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.

 

The clocks

Because of three different clocks, there is the question: What are the differences?

  • std::chrono::sytem_clock: 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: 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 accuracy, the starting point, or the valid time range of the clocks. 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// clockProperties.cpp

#include <chrono>
#include <iomanip>
#include <iostream>

template <typename T>
void printRatio(){ 
    std::cout << "  precision: " << T::num << "/" << T::den << " second " << std::endl;
    typedef typename std::ratio_multiply<T,std::kilo>::type MillSec;
    typedef typename std::ratio_multiply<T,std::mega>::type MicroSec;
    std::cout << std::fixed;
    std::cout << "             " << static_cast<double>(MillSec::num)/MillSec::den << " milliseconds " << std::endl;
    std::cout << "             " << static_cast<double>(MicroSec::num)/MicroSec::den << " microseconds " << std::endl;
}

int main(){
    
    std::cout << std::boolalpha << std::endl;
    
    std::cout << "std::chrono::system_clock: " << std::endl;
    std::cout << "  is steady: " << std::chrono::system_clock::is_steady << std::endl;
    printRatio<std::chrono::system_clock::period>();
    
    std::cout << std::endl;
    
    std::cout << "std::chrono::steady_clock: " << std::endl;
    std::cout << "  is steady: " << std::chrono::steady_clock::is_steady << std::endl;
    printRatio<std::chrono::steady_clock::period>();
    
    std::cout << std::endl;
    
    std::cout << "std::chrono::high_resolution_clock: " << std::endl;
    std::cout << "  is steady: " << std::chrono::high_resolution_clock::is_steady << std::endl;
    printRatio<std::chrono::high_resolution_clock::period>();
    
    
    std::cout << std::endl;
    
}

 

I display in the lines 22, 28, and 34 for each clock whether it's continuous. My job in the function printRatio (lines 7 - 15) is more challenging. First, I show the accuracy of the clocks in a fraction, 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.

 clockPropertiesclockPropertiesWin

Although the C++ standard doesn't specify the epoch of the clock, you can calculate it.

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Stay informed about my mentoring programs. Subscribe for the news.

 

 

Epoch

Thanks to the auxiliary function time_since_epoch, you get from each time point how much time has passed since the epoch.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// now.cpp

#include <chrono>
#include <iomanip>
#include <iostream>

template <typename T>
void durationSinceEpoch(T dur){
    std::cout << "     Counts since epoch:  " << dur.count() << std::endl;
    typedef std::chrono::duration<double, std::ratio<60>> MyMinuteTick;
    MyMinuteTick myMinute(dur);
    std::cout << std::fixed;
    std::cout << "     Minutes since epoch: "<< myMinute.count() << std::endl;
    typedef std::chrono::duration<double, std::ratio<60*60*24*365>> MyYearTick;
    MyYearTick myYear(dur);
    std::cout << "     Years since epoch:   " << myYear.count() << std::endl;
}
    
int main(){
    
    std::cout << std::endl;
    
    std::chrono::system_clock::time_point timeNowSysClock = std::chrono::system_clock::now(); 
    std::chrono::system_clock::duration timeDurSysClock= timeNowSysClock.time_since_epoch();
    std::cout << "std::chrono::system_clock: " << std::endl;
    durationSinceEpoch(timeDurSysClock);
    
    std::cout << std::endl;
     
    auto timeNowStClock = std::chrono::steady_clock::now(); 
    auto timeDurStClock= timeNowStClock.time_since_epoch();
    std::cout << "std::chrono::steady_clock: " << std::endl;
    durationSinceEpoch(timeDurStClock);
    
    std::cout << std::endl;
    
    auto timeNowHiRes = std::chrono::high_resolution_clock::now(); 
    auto timeDurHiResClock= timeNowHiRes.time_since_epoch();
    std::cout << "std::chrono::high_resolution_clock: " << std::endl;
    durationSinceEpoch(timeDurHiResClock);
    
    std::cout << std::endl;

}

 

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 reasons, and my year has 365 days.

The results are different on Linux and Windows.

 

nownowWin

To draw the right conclusion, I have to 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 it 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.

What's next?

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, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, 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, Evangelos Denaxas, 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, and Phillip Diekmann.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, John Nebel, Mipko, and Alicja Kaminska.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

 

Tags: time

Comments   

+2 #1 denis 2016-12-01 07:12
I do not know how to inform you quietly. There is small missprint here.
Quoting Rainer Grimm:
To draw the right conclusion I have to mention that my Linux PC runs for ca 5 hours (305 minutes) and my Windows PC for more the sex hours (391 minutes).
Quote
+2 #2 Rainer Grimm 2016-12-01 18:29
Quoting denis:
I do not know how to inform you quietly. There is small missprint here.
Quoting Rainer Grimm:
To draw the right conclusion I have to mention that my Linux PC runs for ca 5 hours (305 minutes) and my Windows PC for more the sex hours (391 minutes).

Thanks a lot, Denis. I took me more than 2 minutes to see my error.
Quote
0 #3 fun88 2018-06-01 03:50
Great post.
Quote

Mentoring

Stay Informed about my Mentoring

 

English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 4339

Yesterday 5673

Week 22913

Month 43660

All 10965121

Currently are 204 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments