[Python-ideas] add a time decorator to timeit.py
Amjad Ben Hedhili
amjadhedhili at outlook.com
Mon Oct 8 15:46:53 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.
although timeit can be used with a callable, you need to create a lambda expression if the function has args:
def func_to_time(a, b):
timeit.timeit(lambda: func_to_time(a, b), globals=globals())
and you can't use it as a decorator.
De : Python-ideas <python-ideas-bounces+amjadhedhili=outlook.com at python.org> de la part de Jonathan Fine <jfine2358 at gmail.com>
Envoyé : dimanche 7 octobre 2018 09:15
À : python-ideas
Objet : Re: [Python-ideas] add a time decorator to timeit.py
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
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
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
# 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.
Python-ideas mailing list
Python-ideas at python.org
Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas