Skip to content

Commit 884d13a

Browse files
authored
time.clock() now emits a DeprecationWarning (GH-4020)
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.
1 parent de86073 commit 884d13a

File tree

14 files changed

+39
-20
lines changed

14 files changed

+39
-20
lines changed

‎Doc/library/profile.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ procedure can be used to obtain a better constant for a given platform (see
575575
The method executes the number of Python calls given by the argument, directly
576576
and again under the profiler, measuring the time for both. It then computes the
577577
hidden overhead per profiler event, and returns that as a float. For example,
578-
on a 1.8Ghz Intel Core i5 running Mac OS X, and using Python's time.clock() as
578+
on a 1.8Ghz Intel Core i5 running Mac OS X, and using Python's time.process_time() as
579579
the timer, the magical number is about 4.04e-6.
580580

581581
The object of this exercise is to get a fairly consistent result. If your

‎Doc/library/time.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ Functions
289289

290290
.. function:: perf_counter()
291291

292+
.. index::
293+
single: benchmarking
294+
292295
Return the value (in fractional seconds) of a performance counter, i.e. a
293296
clock with the highest available resolution to measure a short duration. It
294297
does include time elapsed during sleep and is system-wide. The reference
@@ -300,6 +303,11 @@ Functions
300303

301304
.. function:: process_time()
302305

306+
.. index::
307+
single: CPU time
308+
single: processor time
309+
single: benchmarking
310+
303311
Return the value (in fractional seconds) of the sum of the system and user
304312
CPU time of the current process. It does not include time elapsed during
305313
sleep. It is process-wide by definition. The reference point of the

‎Lib/ctypes/test/test_numbers.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class c_int_S(_SimpleCData):
241241
def run_test(rep, msg, func, arg=None):
242242
## items = [None] * rep
243243
items = range(rep)
244-
from time import clock
244+
from time import perf_counter as clock
245245
if arg is not None:
246246
start = clock()
247247
for i in items:

‎Lib/ctypes/test/test_strings.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_toolong(self):
194194

195195
def run_test(rep, msg, func, arg):
196196
items = range(rep)
197-
from time import clock
197+
from time import perf_counter as clock
198198
start = clock()
199199
for i in items:
200200
func(arg); func(arg); func(arg); func(arg); func(arg)

‎Lib/profile.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def trace_dispatch(self, frame, event, arg):
195195
self.t = r[0] + r[1] - t # put back unrecorded delta
196196

197197
# Dispatch routine for best timer program (return = scalar, fastest if
198-
# an integer but float works too -- and time.clock() relies on that).
198+
# an integer but float works too -- and time.process_time() relies on that).
199199

200200
def trace_dispatch_i(self, frame, event, arg):
201201
timer = self.timer

‎Lib/test/test_time.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010
import threading
1111
import unittest
12+
import warnings
1213
try:
1314
import _testcapi
1415
except ImportError:
@@ -64,9 +65,11 @@ def test_time(self):
6465
self.assertTrue(info.adjustable)
6566

6667
def test_clock(self):
67-
time.clock()
68+
with self.assertWarns(DeprecationWarning):
69+
time.clock()
6870

69-
info = time.get_clock_info('clock')
71+
with self.assertWarns(DeprecationWarning):
72+
info = time.get_clock_info('clock')
7073
self.assertTrue(info.monotonic)
7174
self.assertFalse(info.adjustable)
7275

@@ -427,8 +430,6 @@ def test_mktime_error(self):
427430
pass
428431
self.assertEqual(time.strftime('%Z', tt), tzname)
429432

430-
@unittest.skipUnless(hasattr(time, 'monotonic'),
431-
'need time.monotonic')
432433
def test_monotonic(self):
433434
# monotonic() should not go backward
434435
times = [time.monotonic() for n in range(100)]
@@ -467,8 +468,6 @@ def test_process_time(self):
467468
self.assertTrue(info.monotonic)
468469
self.assertFalse(info.adjustable)
469470

470-
@unittest.skipUnless(hasattr(time, 'monotonic'),
471-
'need time.monotonic')
472471
@unittest.skipUnless(hasattr(time, 'clock_settime'),
473472
'need time.clock_settime')
474473
def test_monotonic_settime(self):
@@ -506,12 +505,15 @@ def test_localtime_failure(self):
506505
self.assertRaises(ValueError, time.ctime, float("nan"))
507506

508507
def test_get_clock_info(self):
509-
clocks = ['clock', 'perf_counter', 'process_time', 'time']
510-
if hasattr(time, 'monotonic'):
511-
clocks.append('monotonic')
508+
clocks = ['clock', 'monotonic', 'perf_counter', 'process_time', 'time']
512509

513510
for name in clocks:
514-
info = time.get_clock_info(name)
511+
if name == 'clock':
512+
with self.assertWarns(DeprecationWarning):
513+
info = time.get_clock_info('clock')
514+
else:
515+
info = time.get_clock_info(name)
516+
515517
#self.assertIsInstance(info, dict)
516518
self.assertIsInstance(info.implementation, str)
517519
self.assertNotEqual(info.implementation, '')

‎Lib/turtledemo/bytedesign.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""
2424

2525
from turtle import Turtle, mainloop
26-
from time import clock
26+
from time import perf_counter as clock
2727

2828
# wrapper for any additional drawing routines
2929
# that need to know about each other

‎Lib/turtledemo/forest.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414
from turtle import Turtle, colormode, tracer, mainloop
1515
from random import randrange
16-
from time import clock
16+
from time import perf_counter as clock
1717

1818
def symRandom(n):
1919
return randrange(-n,n+1)

‎Lib/turtledemo/fractalcurves.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
scripts for turtle-graphics.
1313
"""
1414
from turtle import *
15-
from time import sleep, clock
15+
from time import sleep, perf_counter as clock
1616

1717
class CurvesTurtle(Pen):
1818
# example derived from

‎Lib/turtledemo/penrose.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818
from turtle import *
1919
from math import cos, pi
20-
from time import clock, sleep
20+
from time import perf_counter as clock, sleep
2121

2222
f = (5**0.5-1)/2.0 # (sqrt(5)-1)/2 -- golden ratio
2323
d = 2 * cos(3*pi/10)

0 commit comments

Comments
 (0)