Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate time.clock() #58517

Closed
vstinner opened this issue Mar 15, 2012 · 10 comments
Closed

Deprecate time.clock() #58517

vstinner opened this issue Mar 15, 2012 · 10 comments
Labels
stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 14309
Nosy @malemburg, @pitrou, @vstinner, @giampaolo

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2012-04-29.00:53:40.332>
created_at = <Date 2012-03-15.00:52:01.905>
labels = ['library']
title = 'Deprecate time.clock()'
updated_at = <Date 2012-04-29.00:54:22.864>
user = 'https://github.com/vstinner'

bugs.python.org fields:

activity = <Date 2012-04-29.00:54:22.864>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2012-04-29.00:53:40.332>
closer = 'python-dev'
components = ['Library (Lib)']
creation = <Date 2012-03-15.00:52:01.905>
creator = 'vstinner'
dependencies = []
files = []
hgrepos = []
issue_num = 14309
keywords = []
message_count = 10.0
messages = ['155831', '155866', '155873', '156063', '156335', '156341', '157519', '157520', '159555', '159556']
nosy_count = 5.0
nosy_names = ['lemburg', 'pitrou', 'vstinner', 'giampaolo.rodola', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue14309'
versions = ['Python 3.3']

@vstinner
Copy link
Member Author

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 bpo-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)

@vstinner vstinner added the stdlib Python modules in the Lib dir label Mar 15, 2012
@malemburg
Copy link
Member

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 bpo-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.

@vstinner
Copy link
Member Author

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.

@malemburg
Copy link
Member

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.

@vstinner
Copy link
Member Author

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?

@malemburg
Copy link
Member

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 ?

@vstinner
Copy link
Member Author

vstinner commented Apr 4, 2012

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).

@vstinner
Copy link
Member Author

vstinner commented Apr 4, 2012

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.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Apr 29, 2012

New changeset 314c3faea2fb by Victor Stinner in branch 'default':
Close bpo-14309: Deprecate time.clock()
http://hg.python.org/cpython/rev/314c3faea2fb

@python-dev python-dev mannequin closed this as completed Apr 29, 2012
@vstinner
Copy link
Member Author

The PEP-418 has been accepted: read it to understand why time.clock() is now deprecated.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

2 participants