classification
Title: Deprecate time.clock()
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: giampaolo.rodola, haypo, lemburg, pitrou, python-dev
Priority: normal Keywords:

Created on 2012-03-15 00:52 by haypo, last changed 2012-04-29 00:54 by haypo. This issue is now closed.

Messages (10)
msg155831 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-03-15 00:52
Python 3.3 has 3 functions to get time:

 - time.clock()
 - time.steady()
 - time.time()

Antoine Pitrou suggested to deprecated time.clock() in msg120149 (issue #10278).

"The problem is time.clock(), since it does two wildly different things
depending on the OS. I would suggest to deprecate time.clock() at the same time as we add time.wallclock(). For the Unix-specific definition of time.clock(), there is already os.times() (which gives even richer information)."

(time.wallclock was the old name of time.steady)
msg155866 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2012-03-15 06:37
STINNER Victor wrote:
> 
> New submission from STINNER Victor <victor.stinner@gmail.com>:
> 
> Python 3.3 has 3 functions to get time:
> 
>  - time.clock()
>  - time.steady()
>  - time.time()
> 
> Antoine Pitrou suggested to deprecated time.clock() in msg120149 (issue #10278).
> 
> "The problem is time.clock(), since it does two wildly different things
> depending on the OS. I would suggest to deprecate time.clock() at the same time as we add time.wallclock(). For the Unix-specific definition of time.clock(), there is already os.times() (which gives even richer information)."
> 
> (time.wallclock was the old name of time.steady)

Strong -1 on this idea.

time.clock() has been in use for ages in many many scripts. We don't
want to carelessly break all those.
msg155873 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-03-15 09:31
> time.clock() has been in use for ages in many many scripts.
> We don't want to carelessly break all those.

I don't want to remove the function, just mark it as deprecated to
avoid confusion. It will only be removed from the next major Python.
msg156063 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2012-03-16 18:05
STINNER Victor wrote:
> 
> STINNER Victor <victor.stinner@gmail.com> added the comment:
> 
>> time.clock() has been in use for ages in many many scripts.
>> We don't want to carelessly break all those.
> 
> I don't want to remove the function, just mark it as deprecated to
> avoid confusion. It will only be removed from the next major Python.

Why ? There's no other single function providing the same functionality,
so it's not even a candidate for deprecation.

Similar functionality is available via several different functions,
but that's true for a lot functions in th stdlib.
msg156335 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-03-19 12:46
> There's no other single function providing the same functionality

time.clock() is not portable: it is a different clock depending on the OS. To write portable code, you have to use the right function:

 - time.time()
 - time.steady()
 - os.times(), resource.getrusage()

On Windows, time.clock() should be replaced by time.steady(). On UNIX, time.clock() can be replaced with "usage=os.times(); usage[0]+usage[1]" for example.

Which kind of use case is not covered by these functions?
msg156341 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2012-03-19 14:14
STINNER Victor wrote:
> 
> STINNER Victor <victor.stinner@gmail.com> added the comment:
> 
>> There's no other single function providing the same functionality
> 
> time.clock() is not portable: it is a different clock depending on the OS. To write portable code, you have to use the right function:
> 
>  - time.time()
>  - time.steady()
>  - os.times(), resource.getrusage()

time.clock() does exactly what the docs say: you get access to
a CPU timer. It's normal that CPU timers work differently on
different OSes.

> On Windows, time.clock() should be replaced by time.steady().

What for ? time.clock() uses the same timer as time.steady() on Windows,
AFAICT, so all you change is the name of the function.

> On UNIX, time.clock() can be replaced with "usage=os.times(); usage[0]+usage[1]" for example.

And what's the advantage of that over using time.clock() directly ?
msg157519 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-04-04 23:43
I misunderstood the time.clock() function. It counts the CPU time while the process is active, whereas a monotonic clock counts elapsed time even during a sleep. time.clock() and time.monotonic() are different clocks for different purposes.

I wrote the PEP 418 which contains a list of all available OS clocks. It lists monotonic clocks, but also "process time" and "thread time" clocks. And it has a "Deferred API: time.perf_counter()" section.

Something can be done to provide portable functions to get the user and system times. See for example Tools/pybench/systimes.py written by Marc-Andre Lemburg.

Python 3.3 gives access to clock_gettime(CLOCK_PROCESS_CPUTIME_ID) and clock_gettime(CLOCK_THREAD_CPUTIME_ID).
msg157520 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-04-04 23:49
> Something can be done to provide portable functions to get
> the user and system times.

Lib/profile.py tests also various functions to choose the best one in Profile constructor.
msg159555 - (view) Author: Roundup Robot (python-dev) Date: 2012-04-29 00:53
New changeset 314c3faea2fb by Victor Stinner in branch 'default':
Close #14309: Deprecate time.clock()
http://hg.python.org/cpython/rev/314c3faea2fb
msg159556 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-04-29 00:54
The PEP 418 has been accepted: read it to understand why time.clock() is now deprecated.
History
Date User Action Args
2012-04-29 00:54:22hayposetmessages: + msg159556
2012-04-29 00:53:40python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg159555

resolution: fixed
stage: resolved
2012-04-04 23:49:30hayposetmessages: + msg157520
2012-04-04 23:43:50hayposetmessages: + msg157519
2012-03-19 14:14:16lemburgsetmessages: + msg156341
2012-03-19 12:46:56hayposetmessages: + msg156335
2012-03-16 18:05:31lemburgsetmessages: + msg156063
2012-03-15 09:36:23giampaolo.rodolasetnosy: + giampaolo.rodola
2012-03-15 09:31:29hayposetmessages: + msg155873
2012-03-15 06:37:48lemburgsetnosy: + lemburg
messages: + msg155866
2012-03-15 00:52:01haypocreate