Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have timeit warn about runs that are not independent of each other #67740

Closed
rhettinger opened this issue Feb 28, 2015 · 10 comments
Closed

Have timeit warn about runs that are not independent of each other #67740

rhettinger opened this issue Feb 28, 2015 · 10 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

BPO 23552
Nosy @arigo, @rhettinger, @rbtcollins, @alex, @serhiy-storchaka
Files
  • timeit_warning.diff: Timeit warning
  • timeit_python_warning_2.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2015-08-26.00:41:52.843>
    created_at = <Date 2015-02-28.20:42:51.989>
    labels = ['type-feature', 'library']
    title = 'Have timeit warn about runs that are not independent of each other'
    updated_at = <Date 2015-08-26.00:41:52.828>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2015-08-26.00:41:52.828>
    actor = 'rbcollins'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-08-26.00:41:52.843>
    closer = 'rbcollins'
    components = ['Library (Lib)']
    creation = <Date 2015-02-28.20:42:51.989>
    creator = 'rhettinger'
    dependencies = []
    files = ['38294', '38533']
    hgrepos = []
    issue_num = 23552
    keywords = ['patch']
    message_count = 10.0
    messages = ['236908', '236944', '237012', '237013', '237022', '238341', '238345', '238356', '238357', '249170']
    nosy_count = 7.0
    nosy_names = ['arigo', 'rhettinger', 'rbcollins', 'alex', 'fijall', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue23552'
    versions = ['Python 3.6']

    @rhettinger
    Copy link
    Contributor Author

    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."""

    @rhettinger rhettinger added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 28, 2015
    @serhiy-storchaka
    Copy link
    Member

    How it looks?

    @rhettinger
    Copy link
    Contributor Author

    See attached

    @alex
    Copy link
    Member

    alex commented Mar 2, 2015

    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.

    @serhiy-storchaka
    Copy link
    Member

    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?

    @rbtcollins
    Copy link
    Member

    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.

    @rbtcollins
    Copy link
    Member

    Reviewed on rietvald.

    @serhiy-storchaka
    Copy link
    Member

    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

    @serhiy-storchaka
    Copy link
    Member

    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.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 26, 2015

    New changeset 2e9cf58c891d by Robert Collins in branch 'default':
    Issue bpo-23552: Timeit now warns when there is substantial (4x) variance
    https://hg.python.org/cpython/rev/2e9cf58c891d

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants