clockProperties

The Three Clocks

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: 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.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.

     

     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 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.

     clockPropertiesclockPropertiesWin

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

    Epoch

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

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

    The results are different on Linux and Windows.

     

    nownowWin

    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.

    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, 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, 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, Marco Parri Empoli, moon, Philipp Lenk, Hobsbawm, and Charles-Jianye Chen.

    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
    My special thanks to SHAVEDYAKS

    Seminars

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

    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++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *