Skip to content

Convenience API for timeit.main #49691

Closed
Closed

Description

@ncoghlan
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']

    Activity

    1. ncoghlan commented on Mar 8, 2009

      @ncoghlan
      ContributorAuthor

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

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Mar 8, 2009
    3. pitrou commented on Mar 8, 2009

      @pitrou
      Member

      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 :-)

    4. self-assigned this
      on Mar 9, 2009
    5. rhettinger commented on Apr 7, 2009

      @rhettinger
      Contributor

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

    6. removed their assignment
      on Apr 7, 2009
    7. ncoghlan commented on Jun 26, 2012

      @ncoghlan
      ContributorAuthor

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

    8. jmetz commented on Mar 14, 2018

      jmetzmannequin
      Mannequin

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

    9. jmetz commented on Mar 14, 2018

      jmetzmannequin
      Mannequin

      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.

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      easystdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions