This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Have timeit warn about runs that are not independent of each other
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex, arigo, fijall, python-dev, rbcollins, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-02-28 20:42 by rhettinger, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
timeit_warning.diff rhettinger, 2015-03-02 04:24 Timeit warning review
timeit_python_warning_2.diff serhiy.storchaka, 2015-03-17 23:03 review
Messages (10)
msg236908 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-02-28 20:42
IPython 3.0 added a useful feature that we ought to consider for inclusion either in timeit.repeat() or in the command-line interface:

"""Using %timeit prints warnings if there is at least a 4x difference in timings between the slowest and fastest runs, since this might meant that the multiple runs are not independent of one another."""
msg236944 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-01 08:34
How it looks?
msg237012 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-03-02 04:24
See attached
msg237013 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2015-03-02 04:26
This seems like it probably will report something useless (and ultimately be disabled) on PyPy, where runs before and after the JIT will display significant variance.
msg237022 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-02 06:45
May be add an option to control a warning (e.g. turn it off when Python run with -Wignore)? May be write warning to stderr?
msg238341 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-03-17 21:32
I think for PyPI its actually important here - the JIT'd state of the code is essentially global state being mutated - you can't assess how fast the code is without first warming up the JIT, and if it warms up half way through your fastest run, you're still not actually finding out what you might want to find out.

E.g. do you want to know:
 - how fast is this unjitted [e.g. CLI's]
 - how fast will this be once its hot [e.g. services]

Personally, I think as a first approximation, warning about massive variance is a good thing. We could add an option to turn it off, and we could also look at hooking properly into the jit to allow detection of stable state and benchmark only from there on in. But those extra things don't detract from the utility of warning about suspect runs.
msg238345 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-03-17 21:53
Reviewed on rietvald.
msg238356 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-17 22:43
Here is a patch that emits a warning using the warnings module. The warning is output to stderr and can be suppressed with the -Wignore option, as all other warnings.

$ ./python -m timeit -n1 -r 10 -s "import time, random" -- "time.sleep(random.random())"
1 loops, best of 10: 79.6 msec per loop
:0: UserWarning: These test results likely aren't reliable.  The worst
time was more than four times slower than the best time.
$ ./python -Wignore -m timeit -n1 -r 10 -s "import time, random" -- "time.sleep(random.random())"
1 loops, best of 10: 16.2 msec per loop
msg238357 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-17 23:03
Implemented Robert's suggestion.

$ ./python -m timeit -n1 -r 10 -s "import time, random" -- "time.sleep(random.random())"
1 loops, best of 10: 30.2 msec per loop
:0: UserWarning: The test results are likely unreliable. The worst
time (946 msec) was more than four times slower than the best time.
msg249170 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-08-26 00:40
New changeset 2e9cf58c891d by Robert Collins in branch 'default':
Issue #23552: Timeit now warns when there is substantial (4x) variance
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67740
2015-08-26 00:41:52rbcollinssetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.6, - Python 3.5
2015-08-26 00:40:53python-devsetnosy: + python-dev
messages: + msg249170
2015-03-17 23:04:12serhiy.storchakasetfiles: - timeit_python_warning.diff
2015-03-17 23:03:55serhiy.storchakasetfiles: + timeit_python_warning_2.diff

messages: + msg238357
2015-03-17 22:43:54serhiy.storchakasetfiles: + timeit_python_warning.diff

messages: + msg238356
2015-03-17 21:53:54rbcollinssetmessages: + msg238345
stage: patch review
2015-03-17 21:32:11rbcollinssetnosy: + rbcollins
messages: + msg238341
2015-03-02 06:45:55serhiy.storchakasetmessages: + msg237022
2015-03-02 04:26:18alexsetnosy: + alex, arigo, fijall
messages: + msg237013
2015-03-02 04:24:28rhettingersetfiles: + timeit_warning.diff
keywords: + patch
messages: + msg237012
2015-03-01 08:34:29serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg236944
2015-02-28 20:42:51rhettingercreate