You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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=Noneclosed_at=<Date2012-06-26.12:45:22.801>created_at=<Date2009-03-08.06:02:22.454>labels= ['easy', 'type-feature', 'library']
title='Convenience API for timeit.main'updated_at=<Date2018-03-14.15:02:20.949>user='https://github.com/ncoghlan'
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.
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 :-)
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.
Activity
ncoghlan commentedon 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:
The new function would cover the latter section of the current main()
function, starting from the line "t = Timer(stmt, setup, timer)".
pitrou commentedon 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 commentedon Apr 7, 2009
See related discussion in bpo-2527 and bpo-1397474.
ncoghlan commentedon Jun 26, 2012
Close in favour of bpo-6422 - that one at least has a patch :)
jmetz commentedon 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 commentedon 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.