Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.procedure() 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
Expand Down
32 changes: 10 additions & 22 deletions Doc/library/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,6 @@ Functions
trailing newline.


.. function:: clock()

.. index::
single: CPU time
single: processor time
single: benchmarking

On Unix, return the current processor time as a floating point number expressed
in seconds. The precision, and in fact the very definition of the meaning of
"processor time", depends on that of the C function of the same name.

On Windows, this function returns wall-clock seconds elapsed since the first
call to this function, as a floating point number, based on the Win32 function
:c:func:`QueryPerformanceCounter`. The resolution is typically better than one
microsecond.

.. deprecated:: 3.3
The behaviour of this function depends on the platform: use
:func:`perf_counter` or :func:`process_time` instead, depending on your
requirements, to have a well defined behaviour.

.. function:: pthread_getcpuclockid(thread_id)

Return the *clk_id* of the thread-specific CPU-time clock for the specified *thread_id*.
Expand Down Expand Up @@ -219,7 +198,6 @@ Functions
Supported clock names and the corresponding functions to read their value
are:

* ``'clock'``: :func:`time.clock`
* ``'monotonic'``: :func:`time.monotonic`
* ``'perf_counter'``: :func:`time.perf_counter`
* ``'process_time'``: :func:`time.process_time`
Expand All @@ -236,6 +214,8 @@ Functions
- *resolution*: The resolution of the clock in seconds (:class:`float`)

.. versionadded:: 3.3
.. versionchanged:: 3.7
``'clock'`` is no more accepted since ``time.clock()`` was removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/no more/no longer



.. function:: gmtime([secs])
Expand Down Expand Up @@ -289,6 +269,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
Expand All @@ -300,6 +283,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
Expand Down
14 changes: 14 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ string
expression pattern for braced placeholders and non-braced placeholders
separately. (Contributed by Barry Warsaw in :issue:`1198569`.)

time
----

The ``time.clock()`` function has been removed, it was deprecated since Python
3.3. The function was not portable: on Windows it mesured wall-clock, whereas

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps replace:

it was deprecated since Python 3.3.

with:

as it has been deprecated since Python 3.3.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/mesured/measured

it measured CPU time on Unix. Python provides better defined clocks with better
resolution since Python 3.3: use :func:`time.perf_counter` or

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps change:

Python provides better defined clocks with better resolution since Python 3.3

to:

Python has provided other clocks with better resolution since Python 3.3

:func:`time.process_time` instead. (Contributed by Victor Stinner in
:issue:`31803`.)

unittest.mock
-------------

Expand Down Expand Up @@ -540,6 +550,10 @@ that may require changes to your code.
Changes in the Python API
-------------------------

* The ``time.clock()`` function has been removed: use :func:`time.perf_counter` or
:func:`time.process_time` instead. (Contributed by Victor Stinner in
:issue:`31803`.)

* :meth:`pkgutil.walk_packages` now raises ValueError if *path* is a string.
Previously an empty list was returned. (Contributed by Sanyam Khurana in
:issue:`24744`.)
Expand Down
2 changes: 1 addition & 1 deletion Lib/ctypes/test/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Lib/ctypes/test/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_toolong(self):

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)
Expand Down
2 changes: 1 addition & 1 deletion Lib/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def trace_dispatch(self, frame, event, arg):
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
Expand Down
9 changes: 1 addition & 8 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ def test_time(self):
self.assertFalse(info.monotonic)
self.assertTrue(info.adjustable)

def test_clock(self):
time.clock()

info = time.get_clock_info('clock')
self.assertTrue(info.monotonic)
self.assertFalse(info.adjustable)

@unittest.skipUnless(hasattr(time, 'clock_gettime'),
'need time.clock_gettime()')
def test_clock_realtime(self):
Expand Down Expand Up @@ -503,7 +496,7 @@ def test_localtime_failure(self):
self.assertRaises(ValueError, time.ctime, float("nan"))

def test_get_clock_info(self):
clocks = ['clock', 'perf_counter', 'process_time', 'time']
clocks = ['perf_counter', 'process_time', 'time']
if hasattr(time, 'monotonic'):
clocks.append('monotonic')

Expand Down
2 changes: 1 addition & 1 deletion Lib/timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def main(args=None, *, _wrap_timer=None):
try:
opts, args = getopt.getopt(args, "n:u:s:r:tcpvh",
["number=", "setup=", "repeat=",
"time", "clock", "process",
"time", "process",
"verbose", "unit=", "help"])
except getopt.error as err:
print(err)
Expand Down
2 changes: 1 addition & 1 deletion Lib/turtledemo/bytedesign.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""

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
Expand Down
2 changes: 1 addition & 1 deletion Lib/turtledemo/forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""
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)
Expand Down
2 changes: 1 addition & 1 deletion Lib/turtledemo/fractalcurves.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion Lib/turtledemo/penrose.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""
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)
Expand Down
2 changes: 1 addition & 1 deletion Lib/turtledemo/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion Lib/turtledemo/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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]
Expand Down
33 changes: 0 additions & 33 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,6 @@ perf_counter(_Py_clock_info_t *info)
return PyFloat_FromDouble(d);
}

#if defined(MS_WINDOWS) || defined(HAVE_CLOCK)
#define PYCLOCK
static PyObject*
pyclock(_Py_clock_info_t *info)
{
#ifdef MS_WINDOWS
return perf_counter(info);
#else
return floatclock(info);
#endif
}

static PyObject *
time_clock(PyObject *self, PyObject *unused)
{
return pyclock(NULL);
}

PyDoc_STRVAR(clock_doc,
"clock() -> floating point number\n\
\n\
Return the CPU time or real time since the start of the process or since\n\
the first call to clock(). This has as much precision as the system\n\
records.");
#endif

#ifdef HAVE_CLOCK_GETTIME
static PyObject *
time_clock_gettime(PyObject *self, PyObject *args)
Expand Down Expand Up @@ -1102,10 +1076,6 @@ time_get_clock_info(PyObject *self, PyObject *args)

if (strcmp(name, "time") == 0)
obj = floattime(&info);
#ifdef PYCLOCK
else if (strcmp(name, "clock") == 0)
obj = pyclock(&info);
#endif
else if (strcmp(name, "monotonic") == 0)
obj = pymonotonic(&info);
else if (strcmp(name, "perf_counter") == 0)
Expand Down Expand Up @@ -1277,9 +1247,6 @@ PyInit_timezone(PyObject *m) {

static PyMethodDef time_methods[] = {
{"time", time_time, METH_NOARGS, time_doc},
#ifdef PYCLOCK
{"clock", time_clock, METH_NOARGS, clock_doc},
#endif
#ifdef HAVE_CLOCK_GETTIME
{"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc},
#endif
Expand Down