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
27 changes: 12 additions & 15 deletions Doc/library/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,19 @@ Functions

.. function:: clock()

.. index::
single: CPU time
single: processor time
single: benchmarking
Alias to :func:`perf_counter`.

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.
.. deprecated:: 3.3
On Python 3.6 and older, the behaviour of this function depends on the
platform: include time elapsed during sleep on Windows, don't include it
on other platforms.

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.
Use :func:`perf_counter` or :func:`process_time` instead, depending on
your requirements.

.. 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.
.. versionchanged:: 3.7
:func:`time.clock` became an alias to :func:`time.perf_counter` in Python
3.7.

.. function:: pthread_getcpuclockid(thread_id)

Expand Down Expand Up @@ -236,6 +231,8 @@ Functions
- *resolution*: The resolution of the clock in seconds (:class:`float`)

.. versionadded:: 3.3
.. versionchanged:: 3.7
``'clock'`` is now an alias to ``'perf_counter'``.


.. function:: gmtime([secs])
Expand Down
20 changes: 9 additions & 11 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time
import threading
import unittest
import warnings
try:
import _testcapi
except ImportError:
Expand Down Expand Up @@ -65,14 +64,18 @@ def test_time(self):
self.assertTrue(info.adjustable)

def test_clock(self):
with self.assertWarns(DeprecationWarning):
time.clock()
time.clock()

with self.assertWarns(DeprecationWarning):
info = time.get_clock_info('clock')
info = time.get_clock_info('clock')
self.assertTrue(info.monotonic)
self.assertFalse(info.adjustable)

@support.cpython_only
def test_clock_alias(self):
self.assertIs(time.clock, time.perf_counter)
self.assertEqual(time.get_clock_info('clock'),
time.get_clock_info('perf_counter'))

@unittest.skipUnless(hasattr(time, 'clock_gettime'),
'need time.clock_gettime()')
def test_clock_realtime(self):
Expand Down Expand Up @@ -508,12 +511,7 @@ def test_get_clock_info(self):
clocks = ['clock', 'monotonic', 'perf_counter', 'process_time', 'time']

for name in clocks:
if name == 'clock':
with self.assertWarns(DeprecationWarning):
info = time.get_clock_info('clock')
else:
info = time.get_clock_info(name)

info = time.get_clock_info(name)
#self.assertIsInstance(info, dict)
self.assertIsInstance(info.implementation, str)
self.assertNotEqual(info.implementation, '')
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time.clock() is now an alias to time.perf_counter(). On Windows, it has no
effect. On other platforms, it means that time.perf_counter() now includes
time elapsed during sleep.
56 changes: 14 additions & 42 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,39 +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)
{
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
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 @@ -1109,13 +1076,9 @@ 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)
else if (strcmp(name, "perf_counter") == 0 || strcmp(name, "clock") == 0)
obj = perf_counter(&info);
else if (strcmp(name, "process_time") == 0)
obj = py_process_time(&info);
Expand Down Expand Up @@ -1284,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 Expand Up @@ -1363,11 +1323,23 @@ static struct PyModuleDef timemodule = {
PyMODINIT_FUNC
PyInit_time(void)
{
PyObject *m;
PyObject *m, *obj;

m = PyModule_Create(&timemodule);
if (m == NULL)
return NULL;

obj = PyObject_GetAttrString(m, "perf_counter");
if (obj == NULL) {
return NULL;
}

if (PyObject_SetAttrString(m, "clock", obj) < 0) {
Py_DECREF(obj);
return NULL;
}
Py_DECREF(obj);

/* Set, or reset, module variables like time.timezone */
PyInit_timezone(m);

Expand Down