[Python-checkins] peps: PEP 418: Cleanup info on precision

victor.stinner python-checkins at python.org
Sat Apr 14 00:47:32 CEST 2012


http://hg.python.org/peps/rev/47ffc9008585
changeset:   4244:47ffc9008585
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Apr 14 00:47:00 2012 +0200
summary:
  PEP 418: Cleanup info on precision

files:
  pep-0418.txt                |  69 ++++++++++--------------
  pep-0418/clock_precision.py |   2 +
  2 files changed, 30 insertions(+), 41 deletions(-)


diff --git a/pep-0418.txt b/pep-0418.txt
--- a/pep-0418.txt
+++ b/pep-0418.txt
@@ -708,9 +708,10 @@
 =========================  ==========  ===============  =============  ===============
 Name                       Resolution  Adjusted         Include sleep  Include suspend
 =========================  ==========  ===============  =============  ===============
-gethrtime                  1 ns        No               Yes            Yes
+gethrtime()                1 ns        No               Yes            Yes
 CLOCK_HIGHRES              1 ns        No               Yes            ?
 CLOCK_MONOTONIC            1 ns        Slewed on Linux  Yes            No
+CLOCK_MONOTONIC_COARSE     1 ns        Slewed on Linux  Yes            No
 CLOCK_MONOTONIC_RAW        1 ns        No               Yes            No
 CLOCK_BOOTTIME             1 ns        ?                Yes            Yes
 CLOCK_UPTIME               1 ns        No               Yes            ?
@@ -733,13 +734,10 @@
 CLOCK_MONOTONIC            OpenBSD 5.0           10 ms                 7 µs
 QueryPerformanceCounter    Windows Seven         10 ns                 9 µs
 CLOCK_UPTIME               FreeBSD 8.2           11 ns                 9 µs
+CLOCK_MONOTONIC_COARSE     Linux 3.3              1 ms                 1 ms
 GetTickCount               Windows Seven         16 ms                15 ms
 =========================  ================  =========  ===================
 
-For CLOCK_xxx clocks, the precision of this table is the result of
-``clock_getres()``.  It looks like Linux does not implement ``clock_getres()``
-and always returns 1 nanosecond.
-
 mach_absolute_time
 ^^^^^^^^^^^^^^^^^^
 
@@ -761,7 +759,8 @@
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 CLOCK_MONOTONIC and CLOCK_MONOTONIC_RAW represent monotonic time since
-some unspecified starting point.  They cannot be set.
+some unspecified starting point.  They cannot be set.  The precision
+can be read using ``clock_getres()``.
 
 Documentation: refer to the manual page of your operating system.
 Examples:
@@ -800,6 +799,9 @@
 
 CLOCK_MONOTONIC stops while the machine is suspended.
 
+Linux provides also CLOCK_MONOTONIC_COARSE since Linux 2.6.32. It is
+similar to CLOCK_MONOTONIC, less precise but faster.
+
 ``clock_gettime()`` fails if the system does not support the specified
 clock, even if the standard C library supports it.  For example,
 CLOCK_MONOTONIC_RAW requires a kernel version 2.6.28 or later.
@@ -809,16 +811,12 @@
    ``clock_gettime()`` requires to link the program against the rt
    (real-time) library.
 
-.. note::
-
-   Linux provides also CLOCK_MONOTONIC_COARSE since Linux 2.6.32 which
-   has less accuracy than CLOCK_MONOTONIC but is faster.
-
 Windows: QueryPerformanceCounter
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 High-resolution performance counter.  It is monotonic.
-QueryPerformanceFrequency() gives its frequency.
+The frequency of the counter can be read using QueryPerformanceFrequency().
+The precision is 1 / QueryPerformanceFrequency().
 
 It has a much higher resolution, but has lower long term precision
 than GetTickCount() and timeGetTime() clocks.  For example, it will
@@ -884,6 +882,7 @@
 <http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx>`_,
 `GetTickCount64()
 <http://msdn.microsoft.com/en-us/library/windows/desktop/ms724411(v=vs.85).aspx>`_.
+The precision can be read using GetSystemTimeAdjustment().
 
 The elapsed time retrieved by GetTickCount() or GetTickCount64()
 includes time the system spends in sleep or hibernation.
@@ -939,6 +938,8 @@
 created with a clockid_t value of CLOCK_HIGHRES, the system will
 attempt to use an optimal hardware source.
 
+The precision of CLOCK_HIGHRES can be read using ``clock_getres()``.
+
 Solaris: gethrtime
 ^^^^^^^^^^^^^^^^^^
 
@@ -965,6 +966,7 @@
 Name                      Resolution       Include sleep
 ========================= ===============  =============
 CLOCK_REALTIME            1 ns             Yes
+CLOCK_REALTIME_COARSE     1 ns             Yes
 GetSystemTimeAsFileTime   100 ns           Yes
 gettimeofday()            1 µs             Yes
 ftime()                   1 ms             Yes
@@ -980,18 +982,10 @@
 CLOCK_REALTIME             Linux 3.2              1 ns                 2 µs
 CLOCK_REALTIME             FreeBSD 8.2           11 ns                 7 µs
 CLOCK_REALTIME             OpenBSD 5.0           10 ms                10 µs
+CLOCK_REALTIME_COARSE      Linux 3.3              1 ms                 1 ms
 GetSystemTimeAsFileTime    Windows Seven         16 ms                 1 ms
 =========================  ================  =========  ===================
 
-For CLOCK_REALTIME, the precision of this table is the result of
-clock_getres().  It looks like Linux does not implement clock_getres()
-and always returns 1 nanosecond.
-
-.. note::
-
-   Linux provides also CLOCK_REALTIME_COARSE since Linux 2.6.32 which
-   has less accurate than CLOCK_REALTIME but is faster.
-
 
 Windows: GetSystemTimeAsFileTime
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1008,12 +1002,16 @@
 System time on UNIX
 ^^^^^^^^^^^^^^^^^^^
 
-gettimeofday(), ftime(), time() and clock_gettime(CLOCK_REALTIME)
-return the system clock.
+gettimeofday(), ftime(), time() and clock_gettime(CLOCK_REALTIME) return
+the system clock. The precision of CLOCK_REALTIME can be read using
+clock_getres().
 
 The system time can be set using settimeofday() or
 clock_settime(CLOCK_REALTIME).
 
+Linux provides also CLOCK_REALTIME_COARSE since Linux 2.6.32. It is similar
+to CLOCK_REALTIME, less precise but faster.
+
 Alexander Shishkin proposed an API for Linux to be notified when the system
 clock is changed: `timerfd: add TFD_NOTIFY_CLOCK_SET to watch for clock changes
 <http://lwn.net/Articles/432395/>`_ (4th version of the API, March 2011). The
@@ -1058,23 +1056,14 @@
 clock()                    SunOS 5.11             1 µs                10 ms
 =========================  ================  =========  ===================
 
-The precision of clock() in this table is the result of 1 /
-CLOCKS_PER_SEC. The precision of times() in this table is the result of 1 /
-HZ. HZ is a constant or read from sysconf(_SC_CLK_TCK).  For
-CLOCK_PROCESS_CPUTIME_ID, the precision of this table is the result of
-clock_getres().  It looks like Linux does not implement clock_getres() and
-always returns 1 nanosecond.  For GetProcessTimes(), the precision is read
-using GetSystemTimeAdjustment().
-
-
 Functions
 ^^^^^^^^^
 
 * Windows: GetProcessTimes(). The precision can be read using
   GetSystemTimeAdjustment().
 * clock_gettime(CLOCK_PROCESS_CPUTIME_ID): High-resolution per-process
-  timer from the CPU.
-* clock():
+  timer from the CPU. The precision can be read using clock_getres().
+* clock(). The precision is 1 / CLOCKS_PER_SEC.
 
   * Windows: The elapsed wall-clock time since the start of the
     process (elapsed time in seconds times CLOCKS_PER_SEC).  It can
@@ -1082,8 +1071,10 @@
   * UNIX: returns an approximation of processor time used by the
     program.
 
-* times()
-* getrusage(RUSAGE_SELF): ru_utime and ru_stime fields
+* getrusage(RUSAGE_SELF) returns a structure of resource usage of the currenet
+  process.  ru_utime is use CPU time and ru_stime is the system CPU time.
+* times(): structure of process times. The precision is 1 / ticks_per_seconds,
+  where ticks_per_seconds is sysconf(_SC_CLK_TCK) or the HZ constant.
 
 Python source code includes a portable library to get the process time (CPU
 time): `Tools/pybench/systimes.py
@@ -1113,10 +1104,6 @@
 GetThreadTimes()           Windows Seven               16 ms                16 ms
 =========================  ================  ===============  ===================
 
-For CLOCK_THREAD_CPUTIME_ID, the precision of this table is the result
-of clock_getres().  It looks like Linux does not implement
-clock_getres() and always return 1 nanosecond.  For GetThreadTimes(),
-the precision is read using GetSystemTimeAdjustment().
 
 Functions
 ^^^^^^^^^
@@ -1124,7 +1111,7 @@
 * Windows: GetThreadTimes(). The precision can be read using
   GetSystemTimeAdjustment().
 * clock_gettime(CLOCK_THREAD_CPUTIME_ID): Thread-specific CPU-time
-  clock.
+  clock. The precision can be read using of clock_getres().
 
 See also pthread_getcpuclockid().
 
diff --git a/pep-0418/clock_precision.py b/pep-0418/clock_precision.py
--- a/pep-0418/clock_precision.py
+++ b/pep-0418/clock_precision.py
@@ -52,4 +52,6 @@
     def gettime():
         return time.clock_gettime(clock_id)
     test_clock(name, gettime)
+    precision = time.clock_getres(clock_id)
+    print("- announced precision: %s" % format_duration(precision))
 

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


More information about the Python-checkins mailing list