Skip to content

gh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() #116822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ time
instead of the ``GetTickCount64()`` clock which has a resolution of 15.6 ms.
(Contributed by Victor Stinner in :gh:`88494`.)

* On Windows, :func:`time.time()` now uses the
``GetSystemTimePreciseAsFileTime()`` clock to have a resolution better
than 1 us, instead of the ``GetSystemTimeAsFileTime()`` clock which has a
resolution of 15.6 ms.
(Contributed by Victor Stinner in :gh:`63207`.)


tkinter
-------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
On Windows, :func:`time.time()` now uses the
``GetSystemTimePreciseAsFileTime()`` clock to have a resolution better than 1
us, instead of the ``GetSystemTimeAsFileTime()`` clock which has a resolution
of 15.6 ms. Patch by Victor Stinner.
38 changes: 22 additions & 16 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
#endif


#ifdef MS_WINDOWS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think checking for MS_WINDOWS here is sufficient. GetSystemTimePreciseAsFileTime(), QueryPerformanceCounter(), and QueryPerformanceFrequency() are available in the system, app, and game partitions. The old use of GetSystemTimeAdjustment() actually wasn't supported for app builds. However, that function was used improperly anyway.

static _PyTimeFraction py_qpc_base = {0, 0};

// Forward declaration
static int py_win_perf_counter_frequency(_PyTimeFraction *base, int raise_exc);
#endif


static PyTime_t
_PyTime_GCD(PyTime_t x, PyTime_t y)
{
Expand Down Expand Up @@ -895,7 +903,7 @@ py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
FILETIME system_time;
ULARGE_INTEGER large;

GetSystemTimeAsFileTime(&system_time);
GetSystemTimePreciseAsFileTime(&system_time);
large.u.LowPart = system_time.dwLowDateTime;
large.u.HighPart = system_time.dwHighDateTime;
/* 11,644,473,600,000,000,000: number of nanoseconds between
Expand All @@ -904,18 +912,17 @@ py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
PyTime_t ns = large.QuadPart * 100 - 11644473600000000000;
*tp = ns;
if (info) {
DWORD timeAdjustment, timeIncrement;
BOOL isTimeAdjustmentDisabled, ok;
// GetSystemTimePreciseAsFileTime() is implemented using
// QueryPerformanceCounter() internally.
if (py_qpc_base.denom == 0) {
if (py_win_perf_counter_frequency(&py_qpc_base, raise_exc) < 0) {
return -1;
}
}

info->implementation = "GetSystemTimeAsFileTime()";
info->implementation = "GetSystemTimePreciseAsFileTime()";
info->monotonic = 0;
ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
&isTimeAdjustmentDisabled);
if (!ok) {
PyErr_SetFromWindowsErr(0);
return -1;
}
info->resolution = timeIncrement * 1e-7;
info->resolution = _PyTimeFraction_Resolution(&py_qpc_base);
info->adjustable = 1;
}

Expand Down Expand Up @@ -1063,16 +1070,15 @@ py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
{
assert(info == NULL || raise_exc);

static _PyTimeFraction base = {0, 0};
if (base.denom == 0) {
if (py_win_perf_counter_frequency(&base, raise_exc) < 0) {
if (py_qpc_base.denom == 0) {
if (py_win_perf_counter_frequency(&py_qpc_base, raise_exc) < 0) {
return -1;
}
}

if (info) {
info->implementation = "QueryPerformanceCounter()";
info->resolution = _PyTimeFraction_Resolution(&base);
info->resolution = _PyTimeFraction_Resolution(&py_qpc_base);
info->monotonic = 1;
info->adjustable = 0;
}
Expand All @@ -1088,7 +1094,7 @@ py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
"LONGLONG is larger than PyTime_t");
ticks = (PyTime_t)ticksll;

*tp = _PyTimeFraction_Mul(ticks, &base);
*tp = _PyTimeFraction_Mul(ticks, &py_qpc_base);
return 0;
}
#endif // MS_WINDOWS
Expand Down