Measuring with boost :: posix_time :: microsec_clock has an error of more than ten microseconds?

I have the following code:

long long unsigned int GetCurrentTimestamp() { LARGE_INTEGER res; QueryPerformanceCounter(&res); return res.QuadPart; } long long unsigned int initalizeFrequency() { LARGE_INTEGER res; QueryPerformanceFrequency(&res); return res.QuadPart; } //start time stamp boost::posix_time::ptime startTime = boost::posix_time::microsec_clock::local_time(); long long unsigned int start = GetCurrentTimestamp(); // .... // execution that should be measured // .... long long unsigned int end = GetCurrentTimestamp(); boost::posix_time::ptime endTime = boost::posix_time::microsec_clock::local_time(); boost::posix_time::time_duration duration = endTime - startTime; std::cout << "Duration by Boost posix: " << duration.total_microseconds() <<std::endl; std::cout << "Processing time is " << ((end - start) * 1000000 / initalizeFrequency()) << " microsec "<< std::endl; 

Result of this code

 Duration by Boost posix: 0 Processing time is 24 microsec 

Why is there such a big discrepancy? Boost sucks as much as it should measure microseconds, but it measures microseconds with a tenth microsecond error.

+4
source share
2 answers

Posix time: microsec_clock:

Get the UTC time using the second timer clock. On Unix systems, this is implemented using GetTimeOfDay. On most platforms, Win32 is implemented using ftime. Thanks to these APIs, Win32 systems often fail to achieve microsecond resolution. If a higher resolution is critical to your application, check your platform to see the resolution achieved.

ftime just does not provide microsecond resolution. The argument may contain the word microsecond , but the implementation does not provide any precision in this range. This granularity is in ms mode.

You will get something other than ZERO when you need more time, say, more than a minimum of 20 ms.

Edit: Note. Ultimately, the microsec_clock implementation for Windows should use the GetSystemTimePreciseAsFileTime function when (min. Windows 8 desktop, Windows Server 2012 desktop) to achieve the resolution in microseconds.

+4
source

Unfortunately, the current implementation of Boost boost::posix_time::microsec_clock does not use the QueryPerformanceCounter Win32 API, it uses GetSystemTimeAsFileTime , which in turn uses GetSystemTime . But the system time resolution is milliseconds (or even worse).

+2
source

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


All Articles