jmquintana79
9/24/2017 - 9:30 AM

decorator time execution

Decorator to display information of time execution.

## OPTION 1 ##

## time execution decorator (no impact on help() function)
from functools import wraps
def time_dec(func):
    @wraps(func)
    def wrapper(*arg):
        from datetime import datetime, timedelta
        # current time
        tic = datetime.now()
        # function execution
        res = func(*arg)
        # calculate time of processing
        toc = datetime.now()
        tictoc = ((toc-tic).seconds)/60. # minutes
        print ('[Process Time Spend] -- function "%s()" spent %s minutes --'%(func.__name__,tictoc))
        # function return
        return res
    return wrapper


@time_dec
def myFunction(n):
  return None
  
  
## OPTION 2 ##

# from bash, use unix command to return time execution
$time -p python function.py