Skip to content

Commit

Permalink
pythongh-88494: Use GetSystemTimePreciseAsFileTime() in time.time()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Mar 14, 2024
1 parent b54d7c8 commit f6eec65
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
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:`88494`.)


tkinter
-------
Expand Down
@@ -0,0 +1,4 @@
On Windows, :func:`time.time()` now uses the
``GetSystemTimePreciseAsFileTime()`` clock to have a better resolution than
before, instead of the ``GetSystemTimeAsFileTime()`` clock which has a
resolution of 15.6 ms. Patch by Victor Stinner.
30 changes: 15 additions & 15 deletions Python/pytime.c
Expand Up @@ -895,7 +895,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 +904,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 @@ -1057,22 +1056,23 @@ py_win_perf_counter_frequency(_PyTimeFraction *base, int raise_exc)
}


static _PyTimeFraction py_qpc_base = {0, 0};

// N.B. If raise_exc=0, this may be called without the GIL.
static int
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 Down

0 comments on commit f6eec65

Please sign in to comment.