summaryrefslogtreecommitdiff
path: root/Lib/profile.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2005-03-03 11:39:45 +0000
committerMartin v. Löwis <martin@v.loewis.de>2005-03-03 11:39:45 +0000
commita4dac4094acd35810b1497bbf99642fc98afd475 (patch)
treebd486d5e6c438869800f2f4fcc52a4a5a0873b64 /Lib/profile.py
parent7fe60c0a0ae2fe4586491867c902bb13df403285 (diff)
downloadcpython-git-a4dac4094acd35810b1497bbf99642fc98afd475.tar.gz
Patch #645894: Use getrusage for computing the time consumption in
profile.py if available.
Diffstat (limited to 'Lib/profile.py')
-rwxr-xr-xLib/profile.py22
1 files changed, 20 insertions, 2 deletions
diff --git a/Lib/profile.py b/Lib/profile.py
index 8815ac3d6a..5a29bd6568 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -107,6 +107,20 @@ if hasattr(os, "times"):
t = timer()
return t[0] + t[1]
+# Using getrusage(3) is better than clock(3) if available:
+# on some systems (e.g. FreeBSD), getrusage has a higher resolution
+# Furthermore, on a POSIX system, returns microseconds, which
+# wrap around after 36min.
+_has_res = 0
+try:
+ import resource
+ resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF)
+ def _get_time_resource(timer=resgetrusage):
+ t = timer()
+ return t[0] + t[1]
+ _has_res = 1
+except ImportError:
+ pass
class Profile:
"""Profiler class.
@@ -159,8 +173,12 @@ class Profile:
bias = self.bias
self.bias = bias # Materialize in local dict for lookup speed.
- if timer is None:
- if os.name == 'mac':
+ if not timer:
+ if _has_res:
+ self.timer = resgetrusage
+ self.dispatcher = self.trace_dispatch
+ self.get_time = _get_time_resource
+ elif os.name == 'mac':
self.timer = MacOS.GetTicks
self.dispatcher = self.trace_dispatch_mac
self.get_time = _get_time_mac