《python – 实现一个简单的装饰器》— 输出程序运行时间

主要结合程序认识理解Python中的装饰器。练习Python代码的编写。

简单代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#-*- coding: utf-8 -*-
"""
@author: 烽火
@license: Apache Licence
@file: decorate.py
@time: 7/5/17 3:52 PM
"""
import time
"""
定义简单的装饰器,用来输出程序运行的所用时间
"""
def (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;
def printSth(str, count):
for i in range(count):
print("%d hello,%s!"%(i,str))
printSth("world", 100)

运行结果

运行结果