On Mon, 30 Nov 2020 at 03:39, Chris Angelico <ros...@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 <st...@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 <st...@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-statement/ I think it's not a bad idea to have it in timeit. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/KGNVKZEOTGAYPW5CNUU6BVGVGYCW3XKJ/ Code of Conduct: http://python.org/psf/codeofconduct/