]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #645894: Use getrusage for computing the time consumption in
authorMartin v. Löwis <martin@v.loewis.de>
Thu, 3 Mar 2005 11:39:45 +0000 (11:39 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Thu, 3 Mar 2005 11:39:45 +0000 (11:39 +0000)
profile.py if available.

Lib/profile.py
Misc/NEWS

index 8815ac3d6ac0c040cd48e25f61e6a4e1a2db9db2..5a29bd65684310035a300227ae011cd87570d7ef 100755 (executable)
@@ -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
index 708404d2b912586a3528dd71066ff0e25e50be8f..d0a8118caed811936abb38bfa7f890ef31ab99aa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -64,6 +64,9 @@ Extension Modules
 Library
 -------
 
+- Patch #645894: Use getrusage for computing the time consumption in 
+  profile.py if available.
+
 - Patch #1046831: Use get_python_version where appropriate in sysconfig.py.
 
 - Patch #1117454: Remove code to special-case cookies without values