自定义计时器#
为了方便管理日志,可以:
import logging
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s %(name)s %(levelname)s @%(funcName)s: %(message)s")
小技巧
如果想要保存日志,可以
logging.basicConfig(level=logging.DEBUG, filename="draft/main.log", filemode="w",
format="%(asctime)s %(name)s %(levelname)s @%(funcName)s: %(message)s")
创建计时器 TimerContext
实例:
from d2py import TimerContext
# 创建计时器实例
timer = TimerContext("平方函数")
timer
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[2], line 1
----> 1 from d2py import TimerContext
3 # 创建计时器实例
4 timer = TimerContext("平方函数")
ImportError: cannot import name 'TimerContext' from 'd2py' (/opt/hostedtoolcache/Python/3.12.7/x64/lib/python3.12/site-packages/d2py/__init__.py)
定义函数:
@timer
def square(x):
return x**2
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[3], line 1
----> 1 @timer
2 def square(x):
3 return x**2
NameError: name 'timer' is not defined
运行调用函数二次观察计时器的信息:
square(2)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 1
----> 1 square(2)
NameError: name 'square' is not defined
square(3)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[5], line 1
----> 1 square(3)
NameError: name 'square' is not defined
查看计时器记录的运行时间:
timer.times
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 1
----> 1 timer.times
NameError: name 'timer' is not defined
重置计时器,以备开始新的工作:
timer.reset()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[7], line 1
----> 1 timer.reset()
NameError: name 'timer' is not defined
timer.times
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 1
----> 1 timer.times
NameError: name 'timer' is not defined
统计运行时间#
也可以对同一函数调用多次统计平均运行时间:
timer.reset()
for k in range(3):
square(142857)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[9], line 1
----> 1 timer.reset()
2 for k in range(3):
3 square(142857)
NameError: name 'timer' is not defined
查看平均运行时间:
timer.avg()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[10], line 1
----> 1 timer.avg()
NameError: name 'timer' is not defined
查看运行次数:
len(timer)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 1
----> 1 len(timer)
NameError: name 'timer' is not defined
查看累积运行时间:
timer.cumsum()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[12], line 1
----> 1 timer.cumsum()
NameError: name 'timer' is not defined
查看总运行时间:
timer.sum()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[13], line 1
----> 1 timer.sum()
NameError: name 'timer' is not defined
可以使用普通的 Python 函数验证计时器的正确性:
def square(x):
return x**2
%%timeit
square(142857)
68.9 ns ± 0.565 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
当然也可以这样:
with TimerContext("计时器") as cm:
for _ in range(1000000):
square(142857)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[16], line 1
----> 1 with TimerContext("计时器") as cm:
2 for _ in range(1000000):
3 square(142857)
NameError: name 'TimerContext' is not defined