
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)
Classes: Timer Functions: 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)
0.10153918000287376
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 forgiven.
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 https://github.com/python/cpython/commits/3.7/Lib/timeit.py
And also, if you wish, for the tests for timeit.py.