[pypy-commit] pypy py3.3: Implement time.perf_counter().

mjacob noreply at buildbot.pypy.org
Sun Aug 16 20:10:04 CEST 2015


Author: Manuel Jacob <me at manueljacob.de>
Branch: py3.3
Changeset: r78997:4d2cde9414b9
Date: 2015-08-16 20:07 +0200
http://bitbucket.org/pypy/pypy/changeset/4d2cde9414b9/

Log:	Implement time.perf_counter().

diff --git a/pypy/module/time/__init__.py b/pypy/module/time/__init__.py
--- a/pypy/module/time/__init__.py
+++ b/pypy/module/time/__init__.py
@@ -20,6 +20,7 @@
         'sleep' : 'interp_time.sleep',
         '_STRUCT_TM_ITEMS': 'space.wrap(interp_time._STRUCT_TM_ITEMS)',
         'monotonic': 'interp_time.monotonic',
+        'perf_counter': 'interp_time.perf_counter',
     }
 
     if os.name == "posix":
diff --git a/pypy/module/time/interp_time.py b/pypy/module/time/interp_time.py
--- a/pypy/module/time/interp_time.py
+++ b/pypy/module/time/interp_time.py
@@ -4,7 +4,7 @@
 from pypy.interpreter.gateway import unwrap_spec
 from rpython.rtyper.lltypesystem import lltype
 from rpython.rlib.rarithmetic import intmask
-from rpython.rlib.rtime import c_clock_gettime, TIMESPEC
+from rpython.rlib.rtime import c_clock_gettime, TIMESPEC, win_perf_counter
 from rpython.rlib import rposix
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 import math
@@ -751,3 +751,12 @@
     else:
         def monotonic(space):
             return clock_gettime(space, cConfig.CLOCK_MONOTONIC)
+
+
+if _WIN:
+    def perf_counter(space):
+        return space.wrap(win_perf_counter())
+
+else:
+    def perf_counter(space):
+        return monotonic(space)
diff --git a/pypy/module/time/test/test_time.py b/pypy/module/time/test/test_time.py
--- a/pypy/module/time/test/test_time.py
+++ b/pypy/module/time/test/test_time.py
@@ -366,3 +366,7 @@
         time.sleep(0.02)
         t2 = time.monotonic()
         assert t1 < t2
+
+    def test_perf_counter(self):
+        import time
+        assert isinstance(time.perf_counter(), float)
diff --git a/rpython/rlib/rtime.py b/rpython/rlib/rtime.py
--- a/rpython/rlib/rtime.py
+++ b/rpython/rlib/rtime.py
@@ -175,19 +175,22 @@
                            lltype.Void,
                            releasegil=False)
 
+def win_perf_counter():
+    a = lltype.malloc(A, flavor='raw')
+    if state.divisor == 0.0:
+        QueryPerformanceCounter(a)
+        state.counter_start = a[0]
+        QueryPerformanceFrequency(a)
+        state.divisor = float(a[0])
+    QueryPerformanceCounter(a)
+    diff = a[0] - state.counter_start
+    lltype.free(a, flavor='raw')
+    return float(diff) / state.divisor
+
 @replace_time_function('clock')
 def clock():
     if _WIN32:
-        a = lltype.malloc(A, flavor='raw')
-        if state.divisor == 0.0:
-            QueryPerformanceCounter(a)
-            state.counter_start = a[0]
-            QueryPerformanceFrequency(a)
-            state.divisor = float(a[0])
-        QueryPerformanceCounter(a)
-        diff = a[0] - state.counter_start
-        lltype.free(a, flavor='raw')
-        return float(diff) / state.divisor
+        return win_perf_counter()
     elif CLOCK_PROCESS_CPUTIME_ID is not None:
         with lltype.scoped_alloc(TIMESPEC) as a:
             c_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, a)


More information about the pypy-commit mailing list