# -*- coding: utf-8 -*-
'''
定时器 + 装饰器的配合使用来计算某一个函数的运行时间
'''
import threading
import time
import numpy as np
import matplotlib.pyplot as plt
def my_supperTimer(func):
def my_timer():
start = time.time()
func()
end = time.time()
print('程序用时:{} 秒'.format(end - start))
return my_timer
@my_supperTimer
def one():
def my_timer():
print('hello world')
global timer
# timer = threading.Timer(3, my_timer)
# timer.start()
timer = threading.Timer(3, my_timer)
timer.start()
time.sleep(12)
timer.cancel()
return my_timer()
# def one():
# x = np.random.rand(100)
# y = x
# plt.plot(x, y)
# plt.show()
if __name__ == '__main__':
one()
我曾经跨过山和大海,也穿过人山人海,我曾经拥有着的一切,转眼都飘散如烟,我曾经失落失望失掉所有方向,直到看见平凡才是唯一的答案。
——韩寒《平凡之路》
本文介绍了一种使用Python的定时器与装饰器相结合的方法来测量特定函数的运行时间。通过具体实例展示了如何创建一个计时器装饰器,并将其应用于函数上实现自动计时的功能。
1万+

被折叠的 条评论
为什么被折叠?



