[Python-checkins] cpython: time.time() now uses clock_gettime(CLOCK_REALTIME) if available

victor.stinner python-checkins at python.org
Wed Mar 28 02:54:58 CEST 2012


http://hg.python.org/cpython/rev/e505ce0514ff
changeset:   75970:e505ce0514ff
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Mar 28 02:54:15 2012 +0200
summary:
  time.time() now uses clock_gettime(CLOCK_REALTIME) if available

clock_gettime(CLOCK_REALTIME) has a better resolution than gettimeofday().
time.time() falls back on gettimeofday() (and then on other functions) on
error.

files:
  Modules/timemodule.c |  11 +++++++++++
  1 files changed, 11 insertions(+), 0 deletions(-)


diff --git a/Modules/timemodule.c b/Modules/timemodule.c
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -1088,6 +1088,17 @@
 floattime(void)
 {
     _PyTime_timeval t;
+#ifdef HAVE_CLOCK_GETTIME
+    struct timespec tp;
+    int ret;
+
+    /* _PyTime_gettimeofday() does not use clock_gettime()
+       because it would require to link Python to the rt (real-time)
+       library, at least on Linux */
+    ret = clock_gettime(CLOCK_REALTIME, &tp);
+    if (ret == 0)
+        return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
+#endif
     _PyTime_gettimeofday(&t);
     return PyFloat_FromDouble((double)t.tv_sec + t.tv_usec * 1e-6);
 }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list