summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2015-08-26 00:40:28 (GMT)
committerRobert Collins <rbtcollins@hp.com>2015-08-26 00:40:28 (GMT)
commit69de2a576cde2d1cfeac67472ad9d22aa1e87a4b (patch)
tree2e1a50110ebb5cb92caad7d198bf01aed1725b1c
parent5eac15336ab87d864cf5516ecf440ce8d7187fbb (diff)
downloadcpython-69de2a576cde2d1cfeac67472ad9d22aa1e87a4b.zip
cpython-69de2a576cde2d1cfeac67472ad9d22aa1e87a4b.tar.gz
cpython-69de2a576cde2d1cfeac67472ad9d22aa1e87a4b.tar.bz2
Issue #23552: Timeit now warns when there is substantial (4x) variance
between best and worst times. Patch from Serhiy Storchaka.
-rwxr-xr-xLib/timeit.py32
-rw-r--r--Misc/NEWS3
2 files changed, 22 insertions, 13 deletions
diff --git a/Lib/timeit.py b/Lib/timeit.py
index 2de88f7..98cb3eb 100755
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -317,20 +317,26 @@ def main(args=None, *, _wrap_timer=None):
print("%d loops," % number, end=' ')
usec = best * 1e6 / number
if time_unit is not None:
- print("best of %d: %.*g %s per loop" % (repeat, precision,
- usec/units[time_unit], time_unit))
+ scale = units[time_unit]
else:
- if usec < 1000:
- print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
- else:
- msec = usec / 1000
- if msec < 1000:
- print("best of %d: %.*g msec per loop" % (repeat,
- precision, msec))
- else:
- sec = msec / 1000
- print("best of %d: %.*g sec per loop" % (repeat,
- precision, sec))
+ scales = [(scale, unit) for unit, scale in units.items()]
+ scales.sort(reverse=True)
+ for scale, time_unit in scales:
+ if usec >= scale:
+ break
+ print("best of %d: %.*g %s per loop" % (repeat, precision,
+ usec/scale, time_unit))
+ best = min(r)
+ usec = best * 1e6 / number
+ worst = max(r)
+ if worst >= best * 4:
+ usec = worst * 1e6 / number
+ import warnings
+ warnings.warn_explicit(
+ "The test results are likely unreliable. The worst\n"
+ "time (%.*g %s) was more than four times slower than the best time." %
+ (precision, usec/scale, time_unit),
+ UserWarning, '', 0)
return None
if __name__ == "__main__":
diff --git a/Misc/NEWS b/Misc/NEWS
index dabbeea..3404b09 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Core and Builtins
Library
-------
+- Issue #23552: Timeit now warns when there is substantial (4x) variance
+ between best and worst times. Patch from Serhiy Storchaka.
+
- Issue #24633: site-packages/README -> README.txt.
- Issue #24879: help() and pydoc can now list named tuple fields in the