<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 21, 2013 at 10:33 PM, Bryan O'Sullivan <span dir="ltr"><<a href="mailto:bos@serpentine.com" target="_blank">bos@serpentine.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"># HG changeset patch<br>
# User Bryan O'Sullivan <<a href="mailto:bryano@fb.com">bryano@fb.com</a>><br>
# Date 1361482360 28800<br>
# Node ID 95cb8d7754a7a28f52f82a332bf1163eb6dea202<br>
# Parent  19d3300883ce1407d0b527549a82f25d3f182873<br>
util: add a timed function for use during development<br>
<br>
I often want to measure the cost of a function call before/after<br>
an optimization, where using top level "hg --time" timing introduces<br>
enough other noise that I can't tell if my efforts are having an<br>
effect.<br>
<br>
This decorator allows a developer to measure a function's cost with<br>
finer granularity.<br>
<br>
diff --git a/mercurial/util.py b/mercurial/util.py<br>
--- a/mercurial/util.py<br>
+++ b/mercurial/util.py<br>
@@ -1872,3 +1872,45 @@ def isatty(fd):<br>
         return fd.isatty()<br>
     except AttributeError:<br>
         return False<br>
+<br>
+timecount = unitcount(<br>
+    (1, 1e3, _('%.0f s')),<br>
+    (100, 1, _('%.1f s')),<br>
+    (10, 1, _('%.2f s')),<br>
+    (1, 1, _('%.3f s')),<br>
+    (100, 1e-3, _('%.1f ms')),<br>
+    (10, 1e-3, _('%.2f ms')),<br>
+    (1, 1e-3, _('%.3f ms')),<br>
+    (100, 1e-6, _('%.1f us')),<br>
+    (10, 1e-6, _('%.2f us')),<br>
+    (1, 1e-6, _('%.3f us')),<br>
+    (100, 1e-9, _('%.1f ns')),<br>
+    (10, 1e-9, _('%.2f ns')),<br>
+    (1, 1e-9, _('%.3f ns')),<br>
+    )<br>
+<br>
+_timenesting = [0]<br>
+<br>
+def timed(func):<br>
+    '''Report the execution time of a function call to stderr.<br>
+<br>
+    During development, use as a decorator when you need to measure<br>
+    the cost of a function, e.g. as follows:<br>
+<br>
+    @util.timed<br>
+    def foo(a, b, c):<br>
+        pass<br>
+    '''<br>
+<br>
+    def wrapper(*args, **kwargs):<br>
+        start = time.time()<br>
+        _timenesting[0] += 2<br></blockquote><div><br></div><div style>I'm a bit puzzled by the 2. Any hint?</div><div style> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


+        try:<br>
+            return func(*args, **kwargs)<br>
+        finally:<br>
+            elapsed = time.time() - start<br>
+            _timenesting[0] -= 2<br>
+            sys.stderr.write('%s%s: %s\n' %<br>
+                             (' ' * _timenesting[0], func.__name__,<br>
+                              timecount(elapsed)))<br>
+    return wrapper<br>
_______________________________________________<br>
Mercurial-devel mailing list<br>
<a href="mailto:Mercurial-devel@selenic.com">Mercurial-devel@selenic.com</a><br>
<a href="http://selenic.com/mailman/listinfo/mercurial-devel" target="_blank">http://selenic.com/mailman/listinfo/mercurial-devel</a><br>
</blockquote></div><br></div></div>