PEP 418: time.process_time() uses os.times() if available

This commit is contained in:
Victor Stinner 2012-04-13 14:04:37 +02:00
parent 61b5e2476c
commit afc9ba4a20
1 changed files with 30 additions and 22 deletions

View File

@ -213,6 +213,14 @@ Pseudo-code [#pseudo]_::
process_times = win32process.GetProcessTimes(handle)
return (process_times['UserTime'] + process_times['KernelTime']) * 1e-7
else:
import os
try:
import resource
except ImportError:
has_resource = False
else:
has_resource = True
def process_time():
if process_time.use_process_cputime:
try:
@ -220,27 +228,24 @@ Pseudo-code [#pseudo]_::
except OSError:
process_time.use_process_cputime = False
if process_time.use_getrusage:
if process_time.getrusage is None:
try:
import resource
except ImportError:
process_time.use_getrusage = False
else:
def getrusage():
usage = resource.getrusage(resource.RUSAGE_SELF)
return usage[0] + usage[1]
process_time.getrusage = getrusage
if process_time.use_getrusage:
except OSError:
process_time.use_getrusage = False
if process_time.use_times:
try:
return process_time.getrusage()
times = os.times()
return times[0] + times[1]
except OSError:
process_time.use_getrusage = False
return _time.clock()
process_time.use_process_cputime = (
hasattr(time, 'clock_gettime')
and hasattr(time, 'CLOCK_PROCESS_CPUTIME_ID'))
process_time.use_getrusage = True
process_time.getrusage = None
process_time.use_getrusage = has_resource
# On OS/2, only the 5th field of os.times() is set, others are zeros
process_time.use_times = (hasattr(os, 'times') and os.name != 'os2')
Existing functions
@ -1016,6 +1021,7 @@ Name Resolution Include sleep
GetProcessTimes() 100 ns No
CLOCK_PROCESS_CPUTIME_ID 1 ns No
getrusage() 1 µs No
times() \- No
clock() \- No (*)
========================= =============== =============
@ -1032,16 +1038,18 @@ clock() SunOS 5.11 1 µs
getrusage() Linux 3.0 4 ms
clock() FreeBSD 8.2 7.8 ms
clock() Linux 3.2 10 ms
times() Linux 3.0 10 ms
clock() OpenBSD 5.0 10 ms
GetProcessTimes() Windows Seven 15.6 ms
========================= ================ ===============
The precision of clock() in this table is the result of 1 /
CLOCKS_PER_SEC. For CLOCK_PROCESS_CPUTIME_ID, the precision of this
table is the result of clock_getres(). It looks like Linux does not
implement clock_getres() and always returns 1 nanosecond. For
GetProcessTimes(), the precision is read using
GetSystemTimeAdjustment().
CLOCKS_PER_SEC. The precision of times() in this table is the result of 1 /
HZ. HZ is a constant or read from sysconf(_SC_CLK_TCK). For
CLOCK_PROCESS_CPUTIME_ID, the precision of this table is the result of
clock_getres(). It looks like Linux does not implement clock_getres() and
always returns 1 nanosecond. For GetProcessTimes(), the precision is read
using GetSystemTimeAdjustment().
Functions