$python用裝飾器實現一個計時器

直接上代碼:python

import time
from functools import wraps

# 定義裝飾器
def fn_timer(function):
    @wraps(function)
    def function_timer(*args,**kwargs):
        t0 = time.time()
        result = function(*args,**kwargs)
        t1 = time.time()
        print '[finished {func_name} in {time:.2f}s]'.format(func_name = function.__name__,time = t1 - t0)
        return result
    return function_timer

# 使用裝飾器來計時
@fn_timer
def download(url):
    # 模擬下載3秒
    print 'start to download from {0}...'.format(url)
    time.sleep(3)
    print 'download finished!'

download('www.baidu.com')
start to download from www.baidu.com...
download finished!
[finished download in 3.00s]
相關文章
相關標籤/搜索