On Fri, 27 Nov 2020 at 17:11, <sdemen...@gmail.com> wrote: > > When optimizing code, I often need to timeit a part of code or a function. > I am using then the following class > [...] > that I can use either as a context manager in > ``` > with Timer("how long does this take?") as t: > time.sleep(1) > # output: DEBUG:Timer:'how long does this take?' ran in 1.000 seconds > ``` > or as a decorator > ``` > @Timer() > def my_function(): > """this is my function""" > time.sleep(2) > my_function() > # output: DEBUG:Timer:'my_function' ran in 2.000 seconds > ``` > > This class could be added to the timeit module as it fits well its functional > scope.
timeit already has a Timer class that runs the code n times, and other features. I suppose a decorator for timeit should have all the features of timeit.Timer and the same overhead (not more). _______________________________________________ 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/XZWSB4NLJHWBI3N4AIXTNJORGMEF2NUF/ Code of Conduct: http://python.org/psf/codeofconduct/