On Fri, 27 Nov 2020 at 17:11, firstname.lastname@example.org 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).