openSUSE / suse-doc-style-checker

Style Checker for SUSE Documentation
Other
2 stars 5 forks source link

Use Decorator for Benchmarking Functions #131

Open tomschr opened 7 years ago

tomschr commented 7 years ago

Originated from pr #129. See line 396 of __init__.py

If you really need this, you could write something like this:

import time                                                
from functools import wraps

def timeit(func):
   """Decorator for "profiling" and "benchmarking" functions"""
   @wraps(func)
   def timed(*args, **kw):
       start = time.time()
       result = func(*args, **kw)
       end = time.time()

       print('%r (%r, %r) %2.2f sec' % \
              (func.__name__, args, kw, end-start))
       return result

   return timed

See functools.wraps for further information about this function. Use the timeit() decorator as follows:


@timeit
def termcheck(...):
   # ...

Benefits:

The basic structure is shown above. Of course, you can improve this decorator even more. Here are some further ideas: