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.

classification
Title: Convenience API for timeit.main
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: duplicate
Dependencies: Superseder: timeit called from within Python should allow autoranging
View: 6422
Assigned To: Nosy List: eli.bendersky, jmetz, ncoghlan, pitrou, rhettinger, ssapin
Priority: normal Keywords: easy

Created on 2009-03-08 06:02 by ncoghlan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg83305 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2009-03-08 06:02
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)".
msg83309 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-03-08 13:07
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 :-)
msg85688 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-04-07 06:47
See related discussion in issue 2527 and issue 1397474.
msg164069 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-06-26 12:28
Close in favour of #6422 - that one at least has a patch :)
msg313822 - (view) Author: Jeremy Metz (jmetz) Date: 2018-03-14 14:18
Don't understand how #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...?
msg313825 - (view) Author: Jeremy Metz (jmetz) Date: 2018-03-14 15:02
More specifically, #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.
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49691
2018-03-14 15:02:20jmetzsetmessages: + msg313825
2018-03-14 14:18:58jmetzsetnosy: + jmetz
messages: + msg313822
2012-06-26 12:45:22ncoghlansetstatus: open -> closed
2012-06-26 12:28:00ncoghlansetsuperseder: timeit called from within Python should allow autoranging
resolution: duplicate
messages: + msg164069
2012-01-09 11:11:15ssapinsetnosy: + ssapin
2011-07-24 03:08:21eli.benderskysetnosy: + eli.bendersky
2010-08-09 04:12:33terry.reedysetversions: + Python 3.2, - Python 3.1, Python 2.7
2009-04-07 06:47:33rhettingersetassignee: rhettinger ->
messages: + msg85688
2009-03-09 05:29:39rhettingersetassignee: rhettinger

nosy: + rhettinger
2009-03-08 13:07:20pitrousetnosy: + pitrou
messages: + msg83309
2009-03-08 06:02:23ncoghlancreate