]> granicus.if.org Git - python/commitdiff
time.time() now uses clock_gettime(CLOCK_REALTIME) if available
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 28 Mar 2012 00:54:15 +0000 (02:54 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 28 Mar 2012 00:54:15 +0000 (02:54 +0200)
clock_gettime(CLOCK_REALTIME) has a better resolution than gettimeofday().
time.time() falls back on gettimeofday() (and then on other functions) on
error.

Modules/timemodule.c

index c99c0a934b4ec1f322d9907d0bb108d8d713e4c5..f44e0c40c4eb93bba2a51ea6a28e20071c5101a8 100644 (file)
@@ -1088,6 +1088,17 @@ static PyObject*
 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);
 }