Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the precision of high_resolution_clock?

C++11 defines high_resolution_clock and it has the member types period and rep. But I can not figure out how I can get the precision of that clock.

Or, if I may not get to the precision, can I somehow at least get a count in nanoseconds of the minimum representable time duration between ticks? probably using period?

#include <iostream> #include <chrono> void printPrec() {     std::chrono::high_resolution_clock::rep x = 1;     // this is not the correct way to initialize 'period':     //high_resolution_clock::period y = 1;      std::cout << "The smallest period is "               << /* what to do with 'x' or 'y' here? */               << " nanos\n"; } 
like image 667
towi Avatar asked Dec 05 '11 13:12

towi


People also ask

How accurate is Chrono C++?

Even though the precision of the time measurement may be 1 nanosecond, the context switch time for the interrupt corresponding to the packet arrival may be ~1 microsecond.

What is high_ resolution_ clock in c++?

class high_resolution_clock; (since C++11) Class std::chrono::high_resolution_clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.


1 Answers

The minimum representable duration is high_resolution_clock::period::num / high_resolution_clock::period::den seconds. You can print it like this:

std::cout << (double) std::chrono::high_resolution_clock::period::num              / std::chrono::high_resolution_clock::period::den; 

Why is this? A clock's ::period member is defined as "The tick period of the clock in seconds." It is a specialization of std::ratio which is a template to represent ratios at compile-time. It provides two integral constants: num and den, the numerator and denominator of a fraction, respectively.

like image 126
R. Martinho Fernandes Avatar answered Oct 11 '22 15:10

R. Martinho Fernandes