《Python - 實現一個簡單的裝飾器》--- 輸出程序運行時間

主要結合程序認識理解Python中的裝飾器。練習Python代碼的編寫。

簡單代碼

#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
@author: 烽火
@license: Apache Licence 
@file: decorate.py
@time: 7/5/17 3:52 PM
"""
import time

"""
定義簡單的裝飾器,用來輸出程序運行的所用時間
"""
def timer(func):
    def decor(*args):

        start_time = time.time();
        func(*args);
        end_time = time.time();
        d_time = end_time - start_time
        print("run the func use : ", d_time)


    return decor;

@timer  #printSth = timer(printSth) -> printSth = decor
def printSth(str, count):
    for i in range(count):
        print("%d hello,%s!"%(i,str))



printSth("world", 100)

運行結果

運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章