《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)

运行结果

运行结果

发布了170 篇原创文章 · 获赞 55 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/w695050167/article/details/74453073