The correct way to map temporary code using C ++ 11

I'm in the middle of writing some time code for the low latency part of the program.

Looking at what's available in the std :: chrono library, it's hard for me to write a temporary code that is portable.

  • std :: chrono :: high_resolution_clock
  • std :: chrono :: stable_clock
  • stand :: chrono :: system_clock

System_clock is useless as it is unstable and the remaining two hours are problematic.

High_resolution_clock is not necessarily stable on all platforms.

Resistant_click does not necessarily support even-grain resolution time periods (ex: nano seconds)

For my goals, having a stable watch, this is the most important requirement, and I can sort it using microsecond granularity.

My question is, do you need a time code that can be run on different h / w architectures and operating systems - which would be the best option?

+5
source share
1 answer

Use steady_clock . In all implementations, its accuracy is nanoseconds. You can verify this yourself for your platform by printing steady_clock::period::num and steady_clock::period::den .

Now this does not mean that it will actually measure the accuracy of nanoseconds. But platforms do their best. For me, two consecutive calls to steady_clock (with optimizations enabled) will report a time of the order of 100 ns.

 #include "chrono_io.h" #include <chrono> #include <iostream> int main() { using namespace std::chrono; using namespace date; auto t0 = steady_clock::now(); auto t1 = steady_clock::now(); auto t2 = steady_clock::now(); auto t3 = steady_clock::now(); std::cout << t1-t0 << '\n'; std::cout << t2-t1 << '\n'; std::cout << t3-t2 << '\n'; } 

The above example uses a free open source library, only for the header, just for the convenience of formatting the duration. You can format it yourself (I'm lazy). For me, this is just the conclusion:

 287ns 116ns 75ns 

YMMV.

+3
source

Source: https://habr.com/ru/post/1265046/


All Articles