trallard
2/4/2019 - 12:47 PM

timing_decorator

import time


def timeThis(method):
    """Decorator: prints execution time for a given method
    >>> @timeThis
    ... def get_all_employee_details(**kwargs):
    ...     print ('employee details')
    """

    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()

        if "log_time" in kw:
            name = kw.get("log_name", method.__name__.upper())
            kw["log_time"][name] = int((te - ts) * 1000)
        else:
            wtime = (te - ts) * 1000
            print(method.__name__ + f": {wtime:.3f} ms ")
        return result

    return timed