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

Convenience API for timeit.main #49691

Closed
ncoghlan opened this issue Mar 8, 2009 · 6 comments
Closed

Convenience API for timeit.main #49691

ncoghlan opened this issue Mar 8, 2009 · 6 comments
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented Mar 8, 2009

BPO 5441
Nosy @rhettinger, @ncoghlan, @pitrou
Superseder
  • bpo-6422: timeit called from within Python should allow autoranging
  • 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 2012-06-26.12:45:22.801>
    created_at = <Date 2009-03-08.06:02:22.454>
    labels = ['easy', 'type-feature', 'library']
    title = 'Convenience API for timeit.main'
    updated_at = <Date 2018-03-14.15:02:20.949>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2018-03-14.15:02:20.949>
    actor = 'jmetz'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-06-26.12:45:22.801>
    closer = 'ncoghlan'
    components = ['Library (Lib)']
    creation = <Date 2009-03-08.06:02:22.454>
    creator = 'ncoghlan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 5441
    keywords = ['easy']
    message_count = 6.0
    messages = ['83305', '83309', '85688', '164069', '313822', '313825']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'ncoghlan', 'pitrou', 'eli.bendersky', 'ssapin', 'jmetz']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'needs patch'
    status = 'closed'
    superseder = '6422'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5441'
    versions = ['Python 3.2']

    @ncoghlan
    Copy link
    Contributor Author

    ncoghlan commented Mar 8, 2009

    For quick and dirty benchmarking, timeit.main() is one of the handiest
    tools out there, but calling it from Python code is a little tedious
    since you need to construct a fake list of command line arguments in
    order to call it.

    What would be nice is a convenience function that accepted appropriate
    arguments, with timeit.main being refactored to parse the command line
    arguments and then call the new convenience function.

    Possible API:

    def measure(stmt="pass", setup="pass", timer=default_timer,
                repeat=default_repeat, number=default_number,
                verbosity=0, precision=3)

    The new function would cover the latter section of the current main()
    function, starting from the line "t = Timer(stmt, setup, timer)".

    @ncoghlan ncoghlan added stdlib Python modules in the Lib dir easy type-feature A feature request or enhancement labels Mar 8, 2009
    @pitrou
    Copy link
    Member

    pitrou commented Mar 8, 2009

    It would be even better if you could pass a locals or globals
    dictionnary instead of the "setup" arg.
    And even even better if it could implicitly get the locals/globals from
    the calling frame :-)

    @rhettinger rhettinger self-assigned this Mar 9, 2009
    @rhettinger
    Copy link
    Contributor

    See related discussion in bpo-2527 and bpo-1397474.

    @rhettinger rhettinger removed their assignment Apr 7, 2009
    @ncoghlan
    Copy link
    Contributor Author

    Close in favour of bpo-6422 - that one at least has a patch :)

    @jmetz
    Copy link
    Mannequin

    jmetz mannequin commented Mar 14, 2018

    Don't understand how bpo-6422 addresses this - would also like to see a more convenient API for timing; at present CLI gives a nice formatted output, and timeit.timeit gives just the raw timing in seconds.

    Would be easy to implement by simply refactoring main() in to the command line parsing component (first ~ 50 lines from https://github.com/python/cpython/blob/master/Lib/timeit.py#L256 to ~https://github.com/python/cpython/blob/master/Lib/timeit.py#L312 ) and actual timing and printing component, which could then be called via API.

    Am happy to work this up if this can be reopened...?

    @jmetz
    Copy link
    Mannequin

    jmetz mannequin commented Mar 14, 2018

    More specifically, bpo-6422 also mentions at least in part this functionality, but the main focus there seems to be the autorange function.

    Propose splitting these two by reopening this use.

    @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
    easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants