]> granicus.if.org Git - python/commitdiff
time.clock() now emits a DeprecationWarning (GH-4020)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 17 Oct 2017 21:46:45 +0000 (14:46 -0700)
committerGitHub <noreply@github.com>
Tue, 17 Oct 2017 21:46:45 +0000 (14:46 -0700)
bpo-31803: time.clock() and time.get_clock_info('clock') now emit a
DeprecationWarning warning.

Replace time.clock() with time.perf_counter() in tests and demos.

Remove also hasattr(time, 'monotonic') in test_time since time.monotonic()
is now always available since Python 3.5.

14 files changed:
Doc/library/profile.rst
Doc/library/time.rst
Lib/ctypes/test/test_numbers.py
Lib/ctypes/test/test_strings.py
Lib/profile.py
Lib/test/test_time.py
Lib/turtledemo/bytedesign.py
Lib/turtledemo/forest.py
Lib/turtledemo/fractalcurves.py
Lib/turtledemo/penrose.py
Lib/turtledemo/tree.py
Lib/turtledemo/wikipedia.py
Misc/NEWS.d/next/Library/2017-10-17-22-55-13.bpo-31803.YLL1gJ.rst [new file with mode: 0644]
Modules/timemodule.c

index 5796e3acb6a7970b0eda4a03cabf8f4094039708..68f24abe6f0bd7b23a666118f24fd2d881cf4859 100644 (file)
@@ -575,7 +575,7 @@ procedure can be used to obtain a better constant for a given platform (see
 The method executes the number of Python calls given by the argument, directly
 and again under the profiler, measuring the time for both. It then computes the
 hidden overhead per profiler event, and returns that as a float.  For example,
-on a 1.8Ghz Intel Core i5 running Mac OS X, and using Python's time.clock() as
+on a 1.8Ghz Intel Core i5 running Mac OS X, and using Python's time.process_time() as
 the timer, the magical number is about 4.04e-6.
 
 The object of this exercise is to get a fairly consistent result. If your
index 25a4ab508266d38b28026596bfe881e3f336a061..c5d1e83e739b6f01eff9fadc034de23f241a16cf 100644 (file)
@@ -289,6 +289,9 @@ Functions
 
 .. function:: perf_counter()
 
+   .. index::
+      single: benchmarking
+
    Return the value (in fractional seconds) of a performance counter, i.e. a
    clock with the highest available resolution to measure a short duration.  It
    does include time elapsed during sleep and is system-wide.  The reference
@@ -300,6 +303,11 @@ Functions
 
 .. function:: process_time()
 
+   .. index::
+      single: CPU time
+      single: processor time
+      single: benchmarking
+
    Return the value (in fractional seconds) of the sum of the system and user
    CPU time of the current process.  It does not include time elapsed during
    sleep.  It is process-wide by definition.  The reference point of the
index ba4f563357952c6db7088afe780de8da61bac74a..09eef90f748db3dfd89b3c0bbdabc08930e900e1 100644 (file)
@@ -241,7 +241,7 @@ class c_int_S(_SimpleCData):
 def run_test(rep, msg, func, arg=None):
 ##    items = [None] * rep
     items = range(rep)
-    from time import clock
+    from time import perf_counter as clock
     if arg is not None:
         start = clock()
         for i in items:
index c7bfbda73d54a637a834fc1d19ab7b3b22c49def..e28e141394de8abc78f3fa7906ff878518142754 100644 (file)
@@ -194,7 +194,7 @@ class WStringTestCase(unittest.TestCase):
 
 def run_test(rep, msg, func, arg):
     items = range(rep)
-    from time import clock
+    from time import perf_counter as clock
     start = clock()
     for i in items:
         func(arg); func(arg); func(arg); func(arg); func(arg)
index 5ceeddc075fc6be35bdb9065f0f56a64e417a2b6..0340a7907bfd47981d191bb90317295ff4398616 100755 (executable)
@@ -195,7 +195,7 @@ class Profile:
             self.t = r[0] + r[1] - t # put back unrecorded delta
 
     # Dispatch routine for best timer program (return = scalar, fastest if
-    # an integer but float works too -- and time.clock() relies on that).
+    # an integer but float works too -- and time.process_time() relies on that).
 
     def trace_dispatch_i(self, frame, event, arg):
         timer = self.timer
index 61dda09c184d3fa9f7ea924dfdd61ed56cd1ba53..a08fd1822b031c5a0da5b0bb37d1528845e6979b 100644 (file)
@@ -9,6 +9,7 @@ import sysconfig
 import time
 import threading
 import unittest
+import warnings
 try:
     import _testcapi
 except ImportError:
@@ -64,9 +65,11 @@ class TimeTestCase(unittest.TestCase):
         self.assertTrue(info.adjustable)
 
     def test_clock(self):
-        time.clock()
+        with self.assertWarns(DeprecationWarning):
+            time.clock()
 
-        info = time.get_clock_info('clock')
+        with self.assertWarns(DeprecationWarning):
+            info = time.get_clock_info('clock')
         self.assertTrue(info.monotonic)
         self.assertFalse(info.adjustable)
 
@@ -427,8 +430,6 @@ class TimeTestCase(unittest.TestCase):
             pass
         self.assertEqual(time.strftime('%Z', tt), tzname)
 
-    @unittest.skipUnless(hasattr(time, 'monotonic'),
-                         'need time.monotonic')
     def test_monotonic(self):
         # monotonic() should not go backward
         times = [time.monotonic() for n in range(100)]
@@ -467,8 +468,6 @@ class TimeTestCase(unittest.TestCase):
         self.assertTrue(info.monotonic)
         self.assertFalse(info.adjustable)
 
-    @unittest.skipUnless(hasattr(time, 'monotonic'),
-                         'need time.monotonic')
     @unittest.skipUnless(hasattr(time, 'clock_settime'),
                          'need time.clock_settime')
     def test_monotonic_settime(self):
@@ -506,12 +505,15 @@ class TimeTestCase(unittest.TestCase):
         self.assertRaises(ValueError, time.ctime, float("nan"))
 
     def test_get_clock_info(self):
-        clocks = ['clock', 'perf_counter', 'process_time', 'time']
-        if hasattr(time, 'monotonic'):
-            clocks.append('monotonic')
+        clocks = ['clock', 'monotonic', 'perf_counter', 'process_time', 'time']
 
         for name in clocks:
-            info = time.get_clock_info(name)
+            if name == 'clock':
+                with self.assertWarns(DeprecationWarning):
+                    info = time.get_clock_info('clock')
+            else:
+                info = time.get_clock_info(name)
+
             #self.assertIsInstance(info, dict)
             self.assertIsInstance(info.implementation, str)
             self.assertNotEqual(info.implementation, '')
index b3b095b76871af1d413a4e52d8601cbca85e0225..1b7452b512c6eb9a1ff6e47c2da78a63f7946185 100755 (executable)
@@ -23,7 +23,7 @@ mode as fast as possible.
 """
 
 from turtle import Turtle, mainloop
-from time import clock
+from time import perf_counter as clock
 
 # wrapper for any additional drawing routines
 # that need to know about each other
index 7fe080e6333a539ba2bf7d13851c53e7138a2dee..55b7da947d24760b204afc45bff9474b48f45848 100755 (executable)
@@ -13,7 +13,7 @@ http://homepage.univie.ac.at/erich.neuwirth/
 """
 from turtle import Turtle, colormode, tracer, mainloop
 from random import randrange
-from time import clock
+from time import perf_counter as clock
 
 def symRandom(n):
     return randrange(-n,n+1)
index c49f8b88ea128d5e8fd12ba1ea65dcc278b78d07..54ade96a0ad05eba6304b643c21906db609903c4 100755 (executable)
@@ -12,7 +12,7 @@ methods are taken from the PythonCard example
 scripts for turtle-graphics.
 """
 from turtle import *
-from time import sleep, clock
+from time import sleep, perf_counter as clock
 
 class CurvesTurtle(Pen):
     # example derived from
index f73c864b7b40a247028a17474ecec1f2a772aefc..b2a5813938abfb83e300b9105889965e8b21c6e7 100755 (executable)
@@ -17,7 +17,7 @@ For more information see:
 """
 from turtle import *
 from math import cos, pi
-from time import clock, sleep
+from time import perf_counter as clock, sleep
 
 f = (5**0.5-1)/2.0   # (sqrt(5)-1)/2 -- golden ratio
 d = 2 * cos(3*pi/10)
index 71fff355c7cf946e8b2db6c12ec6d62f473c44bd..9998fa839ce1cef1c0646fa1bfddb5f6debb9da6 100755 (executable)
@@ -16,7 +16,7 @@ the current pen is cloned. So in the end
 there are 1024 turtles.
 """
 from turtle import Turtle, mainloop
-from time import clock
+from time import perf_counter as clock
 
 def tree(plist, l, a, f):
     """ plist is list of pens
index d6bbad892db363fbecd4ccbf95c9064d83a8d8d6..47d0f00e9da9d15dc93b0814208949e86be618f5 100644 (file)
@@ -14,7 +14,7 @@ parallel.
 Followed by a complete undo().
 """
 from turtle import Screen, Turtle, mainloop
-from time import clock, sleep
+from time import perf_counter as clock, sleep
 
 def mn_eck(p, ne,sz):
     turtlelist = [p]
diff --git a/Misc/NEWS.d/next/Library/2017-10-17-22-55-13.bpo-31803.YLL1gJ.rst b/Misc/NEWS.d/next/Library/2017-10-17-22-55-13.bpo-31803.YLL1gJ.rst
new file mode 100644 (file)
index 0000000..10a88ba
--- /dev/null
@@ -0,0 +1,2 @@
+time.clock() and time.get_clock_info('clock') now emit a DeprecationWarning
+warning.
index 6af9a90e58aab34370892705b5c27b3bc6dac5db..463f5c55770f239dd8bacf948476b9bee93c1e1c 100644 (file)
@@ -104,6 +104,13 @@ perf_counter(_Py_clock_info_t *info)
 static PyObject*
 pyclock(_Py_clock_info_t *info)
 {
+    if (PyErr_WarnEx(PyExc_DeprecationWarning,
+                      "time.clock has been deprecated in Python 3.3 and will "
+                      "be removed from Python 3.8: "
+                      "use time.perf_counter or time.process_time "
+                      "instead", 1) < 0) {
+        return NULL;
+    }
 #ifdef MS_WINDOWS
     return perf_counter(info);
 #else