> 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 <[email protected]> de
la part de Jonathan Fine <[email protected]>
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
>>> 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.
--
Jonathan
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/