[Python-ideas] add a time decorator to timeit.py

Steven D'Aprano steve at pearwood.info
Sun Oct 7 07:15:27 EDT 2018

On Sun, Oct 07, 2018 at 10:16:08AM +0000, Amjad Ben Hedhili wrote:

> I think that a time decorator will be a useful addition to the sandard 
> library, as i find the current way of measuring execution time a bit 
> tedious:
> timeit.timeit("fun_to_time(a, b)", setup="from __main__ import a, b", number=1)

There are lots of ways to call timeit, but generally if you are calling 
it with number=1 then your results may not be trustworthy. (That depends 
on the function, of course.) There's a reason why timeit defaults to 
number=1000000 rather than 1.

> compared to:
> @timef
> def fun_to_time(a, b):
>     ...

The problem with that as an API is that once you have decorated the 
function to call timeit, how do you use it *without* calling timeit?

If you use functools.wraps, there will be a func_to_time.__wrapped__ but 
I don't think that calling that directly is a good interface.

> or
> timef(print)("Hello world!").
> I already made a basic implementation of it, and it's working well.

Can you explain what the timef decorator does? What arguments does it 
take, how do we use it?


More information about the Python-ideas mailing list