Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2019 01:09
What would you like to do?
int main(){
std::ratio<1, 10> r; //1/10
cout << r.num << "/" << r.den <<endl;
//ALl clock frequency is represented by a ratio, we can print the
//ratio using the similar approach.
cout << chrono::system_clock::period::num <<"/" <<chrono::steady_clock::period::den <<endl;
chrono::microseconds mi(2700);//2700 microseconds!
mi.count(); //get the value 2700 here
//2700000 nanoseconds. The coverssion works fine here for we are converting
//low resolution to high resolution
chrono::nanoseconds na = mi;
na.count(); //get the value 2700000 here
//We need to do the cast here if we want to convert high resolution
//to low resolution. 2 milliseconds, the value is truncated.
chrono::milliseconds mill = chrono::duration_cast<chrono::milliseconds>(mi);
mi = mill + mi; //4700
//Time point//
//current time of system clock
chrono::system_clock::time_point tp = chrono::system_clock::now();
cout << tp.time_since_epoch().count() << endl;
tp = tp + chrono::seconds(2);
cout << tp.time_since_epoch().count() << endl;
//Measure the time span
chrono::steady_clock::time_point start = chrono::steady_clock::now();
cout <<"I am bored!!!" <<endl;
chrono::steady_clock::time_point end = chrono::steady_clock::now();
chrono::steady_clock::duration d = end - start;
if(d == chrono::steady_clock::duration::zero()){
cout << "No time elapsed!" <<endl;
cout << chrono::duration_cast<chrono::microseconds>(d).count() <<endl;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment