Patchwork [01,of,10,v5] util: introduce timer()

login
register
mail settings
Submitter Simon Farnsworth
Date Feb. 15, 2017, 10:06 p.m.
Message ID <36ad17f00656ef853e0b.1487196411@devvm022.lla2.facebook.com>
Download mbox | patch
Permalink /patch/18517/
State Accepted
Headers show

Comments

Simon Farnsworth - Feb. 15, 2017, 10:06 p.m.
# HG changeset patch
# User Simon Farnsworth <simonfar@fb.com>
# Date 1487188439 28800
#      Wed Feb 15 11:53:59 2017 -0800
# Node ID 36ad17f00656ef853e0bd7b79e9cd98b58c92a16
# Parent  afaf3c2b129c8940387fd9928ae4fdc28259d13c
util: introduce timer()

As documented for timeit.default_timer, there are better timers available for
performance measures on some platforms. These timers don't have a set epoch,
and thus are only useful for interval measurements, but have higher
resolution, and thus get you a better measurement overall.

Use the same selection logic as Python's timeit.default_timer. This is a
platform clock on Python 2 and early Python 3, and time.perf_counter on Python
3.3 and later (where time.perf_counter is introduced as the best timer to use).

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1203,8 +1203,13 @@ 
 
 if pycompat.osname == 'nt':
     checkosfilename = checkwinfilename
+    timer = time.clock
 else:
     checkosfilename = platform.checkosfilename
+    timer = time.time
+
+if safehasattr(time, "perf_counter"):
+    timer = time.perf_counter
 
 def makelock(info, pathname):
     try: