Skip to content

Commit 3593351

Browse files
author
loewis
committed
Patch #645894: Use getrusage for computing the time consumption in
profile.py if available. git-svn-id: http://svn.python.org/projects/python/trunk@38547 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 5e26a66 commit 3593351

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

Lib/profile.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ def _get_time_times(timer=os.times):
107107
t = timer()
108108
return t[0] + t[1]
109109

110+
# Using getrusage(3) is better than clock(3) if available:
111+
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
112+
# Furthermore, on a POSIX system, returns microseconds, which
113+
# wrap around after 36min.
114+
_has_res = 0
115+
try:
116+
import resource
117+
resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF)
118+
def _get_time_resource(timer=resgetrusage):
119+
t = timer()
120+
return t[0] + t[1]
121+
_has_res = 1
122+
except ImportError:
123+
pass
110124

111125
class Profile:
112126
"""Profiler class.
@@ -159,8 +173,12 @@ def __init__(self, timer=None, bias=None):
159173
bias = self.bias
160174
self.bias = bias # Materialize in local dict for lookup speed.
161175

162-
if timer is None:
163-
if os.name == 'mac':
176+
if not timer:
177+
if _has_res:
178+
self.timer = resgetrusage
179+
self.dispatcher = self.trace_dispatch
180+
self.get_time = _get_time_resource
181+
elif os.name == 'mac':
164182
self.timer = MacOS.GetTicks
165183
self.dispatcher = self.trace_dispatch_mac
166184
self.get_time = _get_time_mac

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Extension Modules
6464
Library
6565
-------
6666

67+
- Patch #645894: Use getrusage for computing the time consumption in
68+
profile.py if available.
69+
6770
- Patch #1046831: Use get_python_version where appropriate in sysconfig.py.
6871

6972
- Patch #1117454: Remove code to special-case cookies without values

0 commit comments

Comments
 (0)