Liu Hao wrote:
在 2021-04-22 20:35, Christian Franke 写道:
This patch significantly increases the precision of gettimeofday() on Windows 8 or later. It is then similar to Cygwin or Linux.

It also affects C++11 std::chrono:*_clock as _GLIBCXX_USE_GETTIMEOFDAY is defined but not _GLIBCXX_USE_CLOCK_*.

A similar change (or a call to getntptimeofday()) would possibly also make sense for clock_gettime(CLOCK_REALTIME,...).

Drawback: GetSystemTimePreciseAsFileTime() is slower than the legacy function. But it is still reasonably fast. Average time per call on an old machine with i7-2600K:
GetSystemTimePreciseAsFileTime(): 23ns
GetSystemTimeAsFileTime(): <3ns



I don't think the complexity is worth.

Which complexity do you mean - the extra cost of the system call or the extra 15 lines of code?


If users want precision they should use a monotonic clock such as `QueryPerformanceCounter()`. I can't think of any real usage of `gettimeofday()` which needs accurate results.

- The API of gettimeofday() provides microsecond precision.
- All other gettimeofday() implementations I tested (Cygwin, Linux, FreeBSD, ...) provide a resolution better than 1 millisecond. - GetSystemTimePreciseAsFileTime() combines high resolution with wall-clock accuracy for long term measurements, QueryPerformanceCounter() does not. - The resolution of GetSystemTimeAsFileTime() may change unpredictably between >15ms to 1ms because it depends on the minimum of all current "Multimedia Timer" resolutions (timeBegin/EndPeriod()). - This problem is inherited to all C++11 std::chrono::*clock, in particular high_resolution_clock - which definitely is an indirect usage of gettimeofday() where high resolution is expected.

System calls and resolutions of C++11 clocks:

                               Mingw-w64 MSVC16  Cygwin
system_clock::now()               1        2       2
steady_clock::now()               1        3       3
high_resolution_clock::now()      1        3       3

where
1: GetSystemTimeAsFileTime: >15ms to 1ms (unpredictable)
2: GetSystemTimePreciseAsFileTime: <1us
3: QueryPerformanceCounter: <500ns

AFAICS, my patch would switch the Mingw-w64 column to '2'.

BTW: None of the above provide an extra high_resolution_clock:
GNU Libstdc++ Headers: using high_resolution_clock = system_clock;
Cygwin (Clang headers), MSVC16: using high_resolution_clock = steady_clock;

--
Best regards,
Christian Franke



_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to