[Python-ideas] add a time decorator to timeit.py

Jonathan Fine jfine2358 at gmail.com
Sun Oct 7 12:15:03 EDT 2018

Summary: Python's timeit.timeit() has an undocumented feature /
implementation detail that gives much of what the original poster has
asked for. Perhaps revising the docs will solve the problem.

This thread has prompted me to look at timeit again. Usually, I look
at the command line help first.

>>> import timeit
>>> help(timeit)
        timeit(string, string) -> float
        repeat(string, string) -> list
        default_timer() -> float

This time, to my surprise, I found the following works:

>>> def fn(): return 2 + 2
>>> timeit.timeit(fn)

Until today, as I recall, I didn't know this.

Now for: https://docs.python.org/3/library/timeit.html

I don't see any examples there, that show that timeit.timeit can take
a callable as its first argument. So my ignorance can, I hope be

Now for: https://github.com/python/cpython/blob/3.7/Lib/timeit.py#L100

This contains, for both the stmt and setup parameters, explicit tests such as

if isinstance(stmt, str):
    # string case
elif callable(stmt):
    # callable case

So I think it's an undocumented feature, rather than an implementation detail.

And if you're a software historian, now perhaps look at

And also, if you wish, for the tests for timeit.py.


More information about the Python-ideas mailing list