On Mon, 30 Nov 2020 at 03:39, Chris Angelico <rosuav@gmail.com> wrote:
No, Serhiy meant that you can pass a function to timeit.
Aaah, didn't know this. On Mon, 30 Nov 2020 at 12:21, Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, Nov 30, 2020 at 12:11:01AM +0100, Marco Sulla wrote:
You can get the code of a function as a string using `inspect`.
*Sometimes*.
>>> import inspect >>> def func(): ... return 1 ... >>> inspect.getsource(func) Traceback (most recent call last): [...] OSError: could not get source code
So `inspect.getsource()` does not work in the REPL. It does make sense. Anyway, usually I use cProfile, I find the "most slow" function and then I measure the more suspicious lines. So personally I'm more interested in a context manager for timeit than a decorator. I know that PyCharm can report you the speed line per line, but not the free version. On Mon, 30 Nov 2020 at 12:21, Steven D'Aprano <steve@pearwood.info> wrote:
I have been using a variant of this for years now:
https://code.activestate.com/recipes/577896-benchmark-code-with-the-with-sta...
I think it's not a bad idea to have it in timeit.