Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5488449

Browse files
committed
Issue #22043: Simplify time.perf_counter() on Windows
QueryPerformanceFrequency() cannot fail on Windows XP and later according to its documentation: raise an exception on error and drop the fallback to the system clock.
1 parent 0011124 commit 5488449

1 file changed

Lines changed: 16 additions & 30 deletions

File tree

Modules/timemodule.c

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ floatclock(_Py_clock_info_t *info)
9696
#define WIN32_PERF_COUNTER
9797
/* Win32 has better clock replacement; we have our own version, due to Mark
9898
Hammond and Tim Peters */
99-
static int
100-
win_perf_counter(_Py_clock_info_t *info, PyObject **result)
99+
static PyObject*
100+
win_perf_counter(_Py_clock_info_t *info)
101101
{
102102
static LONGLONG cpu_frequency = 0;
103103
static LONGLONG ctrStart;
@@ -109,10 +109,8 @@ win_perf_counter(_Py_clock_info_t *info, PyObject **result)
109109
QueryPerformanceCounter(&now);
110110
ctrStart = now.QuadPart;
111111
if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
112-
/* Unlikely to happen - this works on all intel
113-
machines at least! Revert to clock() */
114-
*result = NULL;
115-
return -1;
112+
PyErr_SetFromWindowsErr(0);
113+
return NULL;
116114
}
117115
cpu_frequency = freq.QuadPart;
118116
}
@@ -124,8 +122,7 @@ win_perf_counter(_Py_clock_info_t *info, PyObject **result)
124122
info->monotonic = 1;
125123
info->adjustable = 0;
126124
}
127-
*result = PyFloat_FromDouble(diff / (double)cpu_frequency);
128-
return 0;
125+
return PyFloat_FromDouble(diff / (double)cpu_frequency);
129126
}
130127
#endif
131128

@@ -135,11 +132,10 @@ static PyObject*
135132
pyclock(_Py_clock_info_t *info)
136133
{
137134
#ifdef WIN32_PERF_COUNTER
138-
PyObject *res;
139-
if (win_perf_counter(info, &res) == 0)
140-
return res;
141-
#endif
135+
return win_perf_counter(info);
136+
#else
142137
return floatclock(info);
138+
#endif
143139
}
144140

145141
static PyObject *
@@ -1036,35 +1032,25 @@ Monotonic clock, cannot go backward.");
10361032
static PyObject*
10371033
perf_counter(_Py_clock_info_t *info)
10381034
{
1039-
#if defined(WIN32_PERF_COUNTER) || defined(PYMONOTONIC)
1040-
PyObject *res;
1041-
#endif
1042-
#if defined(WIN32_PERF_COUNTER)
1043-
static int use_perf_counter = 1;
1044-
#endif
1045-
#ifdef PYMONOTONIC
1046-
static int use_monotonic = 1;
1047-
#endif
1048-
10491035
#ifdef WIN32_PERF_COUNTER
1050-
if (use_perf_counter) {
1051-
if (win_perf_counter(info, &res) == 0)
1052-
return res;
1053-
use_perf_counter = 0;
1054-
}
1055-
#endif
1036+
return win_perf_counter(info);
1037+
#else
10561038

10571039
#ifdef PYMONOTONIC
1040+
static int use_monotonic = 1;
1041+
10581042
if (use_monotonic) {
1059-
res = pymonotonic(info);
1043+
PyObject *res = pymonotonic(info);
10601044
if (res != NULL)
10611045
return res;
10621046
use_monotonic = 0;
10631047
PyErr_Clear();
10641048
}
1049+
#else
1050+
return floattime(info);
10651051
#endif
10661052

1067-
return floattime(info);
1053+
#endif
10681054
}
10691055

10701056
static PyObject *

0 commit comments

Comments
 (0)