[Python-checkins] peps: PEP 418: cleanup pseudo-code; mention clock_getcpuclockid()

victor.stinner python-checkins at python.org
Tue Apr 17 13:42:13 CEST 2012


http://hg.python.org/peps/rev/353f27792430
changeset:   4257:353f27792430
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Apr 17 13:42:05 2012 +0200
summary:
  PEP 418: cleanup pseudo-code; mention clock_getcpuclockid()

 * Write _win_perf_counter() to factorize and simplify clock() and
   process_time() pseudo-code
 * Don't use os.times() but _time.times() to not have to check for the platform
 * clock() retries QueryPerformanceCounter() each time it is called, even if
   it failed

files:
  pep-0418.txt |  65 ++++++++++++++++++---------------------
  1 files changed, 30 insertions(+), 35 deletions(-)


diff --git a/pep-0418.txt b/pep-0418.txt
--- a/pep-0418.txt
+++ b/pep-0418.txt
@@ -183,20 +183,22 @@
 
 Pseudo-code::
 
+    if os.name == 'nt':
+        def _win_perf_counter():
+            if _win_perf_counter.frequency is None:
+                _win_perf_counter.frequency = _time.QueryPerformanceFrequency()
+            return _time.QueryPerformanceCounter() / _win_perf_counter.frequency
+        _win_perf_counter.frequency = None
+
     def perf_counter():
         if perf_counter.use_performance_counter:
-            if perf_counter.performance_frequency is None:
-                try:
-                    perf_counter.performance_frequency = _time.QueryPerformanceFrequency()
-                except OSError:
-                    # QueryPerformanceFrequency() fails if the installed
-                    # hardware does not support a high-resolution performance
-                    # counter
-                    perf_counter.use_performance_counter = False
-                else:
-                    return _time.QueryPerformanceCounter() / perf_counter.performance_frequency
-            else:
-                return _time.QueryPerformanceCounter() / perf_counter.performance_frequency
+            try:
+                return _win_perf_counter()
+            except OSError:
+                # QueryPerformanceFrequency() fails if the installed
+                # hardware does not support a high-resolution performance
+                # counter
+                perf_counter.use_performance_counter = False
         if perf_counter.use_monotonic:
             # The monotonic clock is preferred over the system time
             try:
@@ -205,8 +207,6 @@
                 perf_counter.use_monotonic = False
         return time.time()
     perf_counter.use_performance_counter = (os.name == 'nt')
-    if perf_counter.use_performance_counter:
-        perf_counter.performance_frequency = None
     perf_counter.use_monotonic = hasattr(time, 'monotonic')
 
 
@@ -229,7 +229,6 @@
             process_times = win32process.GetProcessTimes(handle)
             return (process_times['UserTime'] + process_times['KernelTime']) * 1e-7
     else:
-        import os
         try:
             import resource
         except ImportError:
@@ -251,8 +250,9 @@
                     process_time.use_getrusage = False
             if process_time.use_times:
                 try:
-                    times = os.times()
-                    return times[0] + times[1]
+                    times = _time.times()
+                    cpu_time = times.tms_utime + times.tms_stime
+                    return cpu_time / process_time.ticks_per_seconds
                 except OSError:
                     process_time.use_getrusage = False
             return _time.clock()
@@ -260,8 +260,10 @@
             hasattr(time, 'clock_gettime')
             and hasattr(time, 'CLOCK_PROCESS_CPUTIME_ID'))
         process_time.use_getrusage = has_resource
-        # On OS/2, only the 5th field of os.times() is set, others are zeros
-        process_time.use_times = (hasattr(os, 'times') and os.name != 'os2')
+        process_time.use_times = hasattr(_time, 'times')
+        if process_time.use_times:
+            # sysconf("SC_CLK_TCK"), or the HZ constant, or 60
+            process_time.ticks_per_seconds = _times.ticks_per_seconds
 
 
 Existing Functions
@@ -370,22 +372,14 @@
 
     if os.name == 'nt':
         def clock():
-            if clock.use_performance_counter:
-                if clock.perf_frequency is None:
-                    try:
-                        clock.perf_frequency = float(_time.QueryPerformanceFrequency())
-                    except OSError:
-                        # QueryPerformanceFrequency() fails if the installed
-                        # hardware does not support a high-resolution performance
-                        # counter
-                        clock.use_performance_counter = False
-                    else:
-                        return _time.QueryPerformanceCounter() / clock.perf_frequency
-                else:
-                    return _time.QueryPerformanceCounter() / clock.perf_frequency
+            try:
+                return _win_perf_counter()
+            except OSError:
+                # QueryPerformanceFrequency() fails if the installed
+                # hardware does not support a high-resolution performance
+                # counter
+                pass
             return _time.clock()
-        clock.use_performance_counter = True
-        clock.perf_frequency = None
     else:
         clock = _time.clock
 
@@ -1259,7 +1253,8 @@
 
 See also the `QueryProcessCycleTime() function
 <http://msdn.microsoft.com/en-us/library/windows/desktop/ms684929(v=vs.85).aspx>`_
-(sum of the cycle time of all threads).
+(sum of the cycle time of all threads) and `clock_getcpuclockid()
+<http://www.kernel.org/doc/man-pages/online/pages/man3/clock_getcpuclockid.3.html>`_.
 
 
 Thread Time

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


More information about the Python-checkins mailing list