summaryrefslogtreecommitdiffstats
path: root/Python/pytime.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-10-12 15:51:56 (GMT)
committerGitHub <noreply@github.com>2017-10-12 15:51:56 (GMT)
commitcba9a0c6def70549046f1afa6a80e38fe706520e (patch)
tree1700dda3a2a2170cc5d0766387fe6f020540c44c /Python/pytime.c
parent0e61e67a57deb4abc677808201d7cf3c38138e02 (diff)
downloadcpython-cba9a0c6def70549046f1afa6a80e38fe706520e.zip
cpython-cba9a0c6def70549046f1afa6a80e38fe706520e.tar.gz
cpython-cba9a0c6def70549046f1afa6a80e38fe706520e.tar.bz2
bpo-31773: time.perf_counter() uses again double (GH-3964)
time.clock() and time.perf_counter() now use again C double internally. Remove also _PyTime_GetWinPerfCounterWithInfo(): use _PyTime_GetPerfCounterDoubleWithInfo() instead on Windows.
Diffstat (limited to 'Python/pytime.c')
-rw-r--r--Python/pytime.c36
1 files changed, 21 insertions, 15 deletions
diff --git a/Python/pytime.c b/Python/pytime.c
index dbf1dda..7fd2a90 100644
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -801,8 +801,8 @@ _PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
#ifdef MS_WINDOWS
-int
-_PyTime_GetWinPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
+static int
+win_perf_counter(double *tp, _Py_clock_info_t *info)
{
static LONGLONG cpu_frequency = 0;
static LONGLONG ctrStart;
@@ -829,28 +829,33 @@ _PyTime_GetWinPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
}
diff = diff / (double)cpu_frequency;
- return _PyTime_FromDouble(t, diff, _PyTime_ROUND_FLOOR, SEC_TO_NS);
+ *tp = diff;
+ return 0;
}
#endif
int
-_PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
+_PyTime_GetPerfCounterDoubleWithInfo(double *d, _Py_clock_info_t *info)
{
#ifdef MS_WINDOWS
- return _PyTime_GetWinPerfCounterWithInfo(t, info);
+ return win_perf_counter(d, info);
#else
- return _PyTime_GetMonotonicClockWithInfo(t, info);
+ _PyTime_t t;
+ if (_PyTime_GetMonotonicClockWithInfo(&t, info) < 0) {
+ return -1;
+ }
+ *d = _PyTime_AsSecondsDouble(t);
+ return 0;
#endif
}
-_PyTime_t
-_PyTime_GetPerfCounter(void)
+double
+_PyTime_GetPerfCounterDouble(void)
{
- _PyTime_t t;
- if (_PyTime_GetPerfCounterWithInfo(&t, NULL) < 0) {
- /* should not happen, _PyTime_Init() checked the clock at startup */
+ double t;
+ if (_PyTime_GetPerfCounterDoubleWithInfo(&t, NULL)) {
Py_UNREACHABLE();
}
return t;
@@ -860,17 +865,18 @@ _PyTime_GetPerfCounter(void)
int
_PyTime_Init(void)
{
- /* check that the 3 most important clocks are working properly
- to not have to check for exceptions at runtime. If a clock works once,
- it cannot fail in next calls. */
+ /* check that time.time(), time.monotonic() and time.perf_counter() clocks
+ are working properly to not have to check for exceptions at runtime. If
+ a clock works once, it cannot fail in next calls. */
_PyTime_t t;
+ double d;
if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0) {
return -1;
}
if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) {
return -1;
}
- if (_PyTime_GetPerfCounterWithInfo(&t, NULL) < 0) {
+ if (_PyTime_GetPerfCounterDoubleWithInfo(&d, NULL) < 0) {
return -1;
}
return 0;