[Python-checkins] peps: PEP 418: I'm not quite sure, but it looks like QueryPerformanceCounter() *is*

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


http://hg.python.org/peps/rev/269df936b0a4
changeset:   4158:269df936b0a4
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Mar 28 01:45:51 2012 +0200
summary:
  PEP 418: I'm not quite sure, but it looks like QueryPerformanceCounter() *is* monotonic

files:
  pep-0418.txt |  43 +++++++++++++++++++++++++++++----------
  1 files changed, 32 insertions(+), 11 deletions(-)


diff --git a/pep-0418.txt b/pep-0418.txt
--- a/pep-0418.txt
+++ b/pep-0418.txt
@@ -62,7 +62,7 @@
 administrator or automatically by a NTP daemon.  It can jump backward and
 forward, and is not monotonic.
 
-It is avaialble on all platforms and cannot fail.
+It is available on all platforms and cannot fail.
 
 Pseudo-code::
 
@@ -104,30 +104,47 @@
 
     if os.name == 'nt':
         if hasattr(time, '_GetTickCount64'):
-            monotonic = _time.GetTickCount64
+            _get_tick_count = _time.GetTickCount64
         else:
-            def monotonic():
+            def _get_tick_count():
                 ticks = _time.GetTickCount()
-                if ticks < monotonic.last:
+                if ticks < _get_tick_count.last:
                     # Integer overflow detected
-                    monotonic.delta += 2**32
-                monotonic.last = ticks
-                return ticks + monotonic.delta
-            monotonic.last = 0
-            monotonic.delta = 0
-    if os.name == 'mac':
+                    _get_tick_count.delta += 2**32
+                _get_tick_count.last = ticks
+                return ticks + _get_tick_count.delta
+            _get_tick_count.last = 0
+            _get_tick_count.delta = 0
+
+        def monotonic():
+            if monotonic.use_performance_counter:
+                try:
+                    return _time.QueryPerformanceCounter()
+                except OSError:
+                    # QueryPerformanceFrequency() may fail, if the installed
+                    # hardware does not support a high-resolution performance
+                    # counter for example
+                    monotonic.use_performance_counter = False
+            # Fallback to GetTickCount/GetTickCount64 which has
+            # a lower resolution
+            return _get_tick_count()
+        monotonic.use_performance_counter = True
+
+    elif os.name == 'mac':
         def monotonic():
             if monotonic.factor is None:
                 factor = _time.mach_timebase_info()
                 monotonic.factor = timebase[0] / timebase[1]
             return _time.mach_absolute_time() * monotonic.factor
         monotonic.factor = None
+
     elif hasattr(time, "clock_gettime") and hasattr(time, "CLOCK_MONOTONIC"):
         def monotonic():
             if monotonic.use_monotonic_raw:
                 try:
                     return time.clock_gettime(time.CLOCK_MONOTONIC_RAW)
                 except OSError:
+                    # CLOCK_MONOTONIC_RAW requires a Linux kernel >= 2.6.28
                     monotonic.use_monotonic_raw = False
             return time.clock_gettime(time.CLOCK_MONOTONIC)
         monotonic.use_monotonic_raw = hasattr(time, "CLOCK_MONOTONIC_RAW")
@@ -320,7 +337,7 @@
 multiprocessor computer, QueryPerformanceCounter() returned a different value
 for each processor.
 
-QueryPerformanceCounter() is not monotonic.
+QueryPerformanceCounter() is monotonic.
 
 QueryPerformanceFrequency() fails if the installed hardware does not support a
 high-resolution performance counter.
@@ -395,4 +412,8 @@
    <http://qt-project.org/doc/qt-4.8/qelapsedtimer.html>`_
  * `Windows: Game Timing and Multicore Processors
    <http://msdn.microsoft.com/en-us/library/ee417693.aspx>`_
+ * `Implement a Continuously Updating, High-Resolution Time Provider for Windows
+   <http://msdn.microsoft.com/en-us/magazine/cc163996.aspx>`_
+ * `Perl: Time::HiRes
+   <http://perldoc.perl.org/Time/HiRes.html>`_
 

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


More information about the Python-checkins mailing list