forked from jackzhenguo/python-small-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_decorate.py
More file actions
35 lines (26 loc) · 910 Bytes
/
time_decorate.py
File metadata and controls
35 lines (26 loc) · 910 Bytes
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
35
# 测试函数执行时间的装饰器示例
import time
def timing_func(fn):
def wrapper():
start = time.time()
fn() # 执行传入的fn参数
stop = time.time()
return (stop-start)
return wrapper
@timing_func
def test_list_append():
lst = []
for i in range(0, int(1e6)):
lst.append(i)
@timing_func
def test_list_compre():
[i for i in range(0, int(1e6))] # 列表生成式
a = test_list_append()
c = test_list_compre()
print("test list append time:", a)
print("test list comprehension time:", c)
print("append/compre:", round(a/c, 3))
# @timing_func修饰test_list_append意味着完成两步操作,
# 将test_list_append作为timing_func()的参数即执行timing_func(test_list_append),
# 然后将test_list_append替换成timing_func()返回的结果。
# 即被修饰的函数总是被替换成@符号所引用的函数返回值。