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 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 clocks 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 wheter it's continuous. My job in the function printRatio (line 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.

Epoch

Thanks to the auxiliary function time_since_epoch you get from each time point who 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 the explicit types of the time point and time duration are extremely verbose to write. I show the function durationSinceEpoch (line 7 - 17) I the time duration in different resolutions. First, I display the number of time ticks (line 9), than the number of minutes (line 13), and at the end the years (lines 16) since the epoch; all depending on the used clock. For simplicity reasons I ignore leap years 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. You can put with the components time point and time duration a thread for an absolute or relative time to sleep. The details will follow in the next post.  

 

 

 

 

 

title page smalltitle page small Go to Leanpub/cpplibrary "What every professional C++ programmer should know about the C++ standard library".   Get your e-book. Support my blog.

 

Tags: time

Comments   

0 #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
0 #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

Add comment


My Newest E-Book

Latest comments

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 90

All 333381

Currently are 142 guests and no members online