PEP 418: time.process_time() uses os.times() if available
This commit is contained in:
parent
61b5e2476c
commit
afc9ba4a20
52
pep-0418.txt
52
pep-0418.txt
|
@ -213,6 +213,14 @@ Pseudo-code [#pseudo]_::
|
||||||
process_times = win32process.GetProcessTimes(handle)
|
process_times = win32process.GetProcessTimes(handle)
|
||||||
return (process_times['UserTime'] + process_times['KernelTime']) * 1e-7
|
return (process_times['UserTime'] + process_times['KernelTime']) * 1e-7
|
||||||
else:
|
else:
|
||||||
|
import os
|
||||||
|
try:
|
||||||
|
import resource
|
||||||
|
except ImportError:
|
||||||
|
has_resource = False
|
||||||
|
else:
|
||||||
|
has_resource = True
|
||||||
|
|
||||||
def process_time():
|
def process_time():
|
||||||
if process_time.use_process_cputime:
|
if process_time.use_process_cputime:
|
||||||
try:
|
try:
|
||||||
|
@ -220,27 +228,24 @@ Pseudo-code [#pseudo]_::
|
||||||
except OSError:
|
except OSError:
|
||||||
process_time.use_process_cputime = False
|
process_time.use_process_cputime = False
|
||||||
if process_time.use_getrusage:
|
if process_time.use_getrusage:
|
||||||
if process_time.getrusage is None:
|
try:
|
||||||
try:
|
usage = resource.getrusage(resource.RUSAGE_SELF)
|
||||||
import resource
|
return usage[0] + usage[1]
|
||||||
except ImportError:
|
except OSError:
|
||||||
process_time.use_getrusage = False
|
process_time.use_getrusage = False
|
||||||
else:
|
if process_time.use_times:
|
||||||
def getrusage():
|
try:
|
||||||
usage = resource.getrusage(resource.RUSAGE_SELF)
|
times = os.times()
|
||||||
return usage[0] + usage[1]
|
return times[0] + times[1]
|
||||||
process_time.getrusage = getrusage
|
except OSError:
|
||||||
if process_time.use_getrusage:
|
process_time.use_getrusage = False
|
||||||
try:
|
|
||||||
return process_time.getrusage()
|
|
||||||
except OSError:
|
|
||||||
process_time.use_getrusage = False
|
|
||||||
return _time.clock()
|
return _time.clock()
|
||||||
process_time.use_process_cputime = (
|
process_time.use_process_cputime = (
|
||||||
hasattr(time, 'clock_gettime')
|
hasattr(time, 'clock_gettime')
|
||||||
and hasattr(time, 'CLOCK_PROCESS_CPUTIME_ID'))
|
and hasattr(time, 'CLOCK_PROCESS_CPUTIME_ID'))
|
||||||
process_time.use_getrusage = True
|
process_time.use_getrusage = has_resource
|
||||||
process_time.getrusage = None
|
# 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
|
Existing functions
|
||||||
|
@ -1016,6 +1021,7 @@ Name Resolution Include sleep
|
||||||
GetProcessTimes() 100 ns No
|
GetProcessTimes() 100 ns No
|
||||||
CLOCK_PROCESS_CPUTIME_ID 1 ns No
|
CLOCK_PROCESS_CPUTIME_ID 1 ns No
|
||||||
getrusage() 1 µs No
|
getrusage() 1 µs No
|
||||||
|
times() \- No
|
||||||
clock() \- No (*)
|
clock() \- No (*)
|
||||||
========================= =============== =============
|
========================= =============== =============
|
||||||
|
|
||||||
|
@ -1032,16 +1038,18 @@ clock() SunOS 5.11 1 µs
|
||||||
getrusage() Linux 3.0 4 ms
|
getrusage() Linux 3.0 4 ms
|
||||||
clock() FreeBSD 8.2 7.8 ms
|
clock() FreeBSD 8.2 7.8 ms
|
||||||
clock() Linux 3.2 10 ms
|
clock() Linux 3.2 10 ms
|
||||||
|
times() Linux 3.0 10 ms
|
||||||
clock() OpenBSD 5.0 10 ms
|
clock() OpenBSD 5.0 10 ms
|
||||||
GetProcessTimes() Windows Seven 15.6 ms
|
GetProcessTimes() Windows Seven 15.6 ms
|
||||||
========================= ================ ===============
|
========================= ================ ===============
|
||||||
|
|
||||||
The precision of clock() in this table is the result of 1 /
|
The precision of clock() in this table is the result of 1 /
|
||||||
CLOCKS_PER_SEC. For CLOCK_PROCESS_CPUTIME_ID, the precision of this
|
CLOCKS_PER_SEC. The precision of times() in this table is the result of 1 /
|
||||||
table is the result of clock_getres(). It looks like Linux does not
|
HZ. HZ is a constant or read from sysconf(_SC_CLK_TCK). For
|
||||||
implement clock_getres() and always returns 1 nanosecond. For
|
CLOCK_PROCESS_CPUTIME_ID, the precision of this table is the result of
|
||||||
GetProcessTimes(), the precision is read using
|
clock_getres(). It looks like Linux does not implement clock_getres() and
|
||||||
GetSystemTimeAdjustment().
|
always returns 1 nanosecond. For GetProcessTimes(), the precision is read
|
||||||
|
using GetSystemTimeAdjustment().
|
||||||
|
|
||||||
|
|
||||||
Functions
|
Functions
|
||||||
|
|
Loading…
Reference in New Issue