changeset: 97511:2e9cf58c891d user: Robert Collins date: Wed Aug 26 12:40:28 2015 +1200 files: Lib/timeit.py Misc/NEWS description: Issue #23552: Timeit now warns when there is substantial (4x) variance between best and worst times. Patch from Serhiy Storchaka. diff -r 2fca6a5137a5 -r 2e9cf58c891d Lib/timeit.py --- a/Lib/timeit.py Tue Aug 25 17:21:54 2015 -0700 +++ b/Lib/timeit.py Wed Aug 26 12:40:28 2015 +1200 @@ -317,20 +317,26 @@ 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 -r 2fca6a5137a5 -r 2e9cf58c891d Misc/NEWS --- a/Misc/NEWS Tue Aug 25 17:21:54 2015 -0700 +++ b/Misc/NEWS Wed Aug 26 12:40:28 2015 +1200 @@ -17,6 +17,9 @@ 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